本文介绍了在 pandas 图上添加小刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
from pandas_datareader import data as web
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2, 1)
df = web.DataReader('F', 'yahoo')
df2 = web.DataReader('Fb', 'yahoo')
ax = df.plot(figsize=(35,15), ax=ax1)
df2.plot(y = 'Close', figsize=(35,15), ax=ax2)
plt.show()
此产生如下所示的图表:
This produces the chart which looks like this:
我如何更改熊猫图中的次刻度,以便产生如下所示的x轴:
How can i change the minor ticks in pandas plot so it produces the x axis which looks like this:
推荐答案
检查此代码:
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import matplotlib.dates as md
fig, (ax1, ax2) = plt.subplots(2, 1)
df = web.DataReader('F', 'yahoo')
df2 = web.DataReader('Fb', 'yahoo')
ax = df.plot(figsize=(35,15), ax=ax1)
df2.plot(y = 'Close', figsize=(35,15), ax=ax2)
for ax in (ax1, ax2):
ax.xaxis.set_major_locator(md.MonthLocator(bymonth = range(1, 13, 6)))
ax.xaxis.set_major_formatter(md.DateFormatter('%b\n%Y'))
ax.xaxis.set_minor_locator(md.MonthLocator())
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 0 )
plt.show()
您可以管理 xticks
和 ax.xaxis
方法。上面的代码产生了这个图:
You can manage the xticks
with the ax.xaxis
methods. The above code produce this plot:
这篇关于在 pandas 图上添加小刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!