简体   繁体   English

使用matplotlib绘制网格时没有垂直线

[英]No vertical lines when drawing the grid using matplotlib

When I drawing pyploy.subplots from my DataFrame I have not vertical lines. 当我从DataFrame绘制pyploy.subplots时,我没有垂直线。 DataFrame index look that: DataFrame索引看起来:

df_month.index
DatetimeIndex(['2016-01-31', '2016-02-29', '2016-03-31', '2016-04-30',
               '2016-05-31', '2016-06-30', '2016-07-31', '2016-08-31',
               '2016-09-30', '2016-10-31', '2016-11-30', '2016-12-31'],
              dtype='datetime64[ns]', freq='M')

I need vertical line for every row. 我需要每行垂直线。 Actually my figure looks that(when I increase time interval, there is vertical line for every January's indexes): 实际上,我的数字看起来是这样的(当我增加时间间隔时,每个一月的指标都有一条垂直线): 在此处输入图片说明

It is a code for it: 它是一个代码:

seaborn.set()
fig, axis = plt.subplots(nrows=6, sharex=True)

df_month.sum(axis=1).plot(ax=axis[0], marker = '.')

axis[0].set_title('Sum of costs')

for index, category in enumerate(df_month.columns.unique(), start=1):
    df_month[category].sum(axis=1).plot(ax=axis[index], marker = '.')
    axis[index].set_title(category)

plt.show()

You have to plot grid lines for minor and major ticks individually for every axes object ax with commands: 您必须使用以下命令分别为每个轴对象ax绘制次要和主要刻度线的网格线:

ax.grid(b='on', which='major', color='k', linewidth=.5)
ax.grid(b='on', which='minor', color='k', linewidth=.25)

You have to implement this code in a cycle over all axes objects of your subplots. 您必须在子图的所有轴对象上循环执行此代码。

Look for more info here: https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.grid.html 在此处查找更多信息: https : //matplotlib.org/api/_as_gen/matplotlib.axes.Axes.grid.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM