使垂直网格线出现在matplotlib中的线图中 [英] Getting vertical gridlines to appear in line plot in matplotlib
问题描述
我想在绘图上同时获得水平和垂直网格线,但是默认情况下只显示水平网格线.我正在使用python中的sql查询中的pandas.DataFrame
来生成在x轴上具有日期的折线图.我不确定为什么它们没有出现在日期上,我试图寻找答案,但是找不到答案.
I want to get both horizontal and vertical grid lines on my plot but only the horizontal grid lines are appearing by default. I am using a pandas.DataFrame
from an sql query in python to generate a line plot with dates on the x-axis. I'm not sure why they do not appear on the dates and I have tried to search for an answer to this but couldn't find one.
我用来绘制图形的全部是下面的简单代码.
All I have used to plot the graph is the simple code below.
data.plot()
grid('on')
data是DataFrame,其中包含日期和来自sql查询的数据.
data is the DataFrame which contains the dates and the data from the sql query.
我也尝试过添加下面的代码,但是我仍然得到相同的输出,但没有垂直网格线.
I have also tried adding the code below but I still get the same output with no vertical grid lines.
ax = plt.axes()
ax.yaxis.grid() # horizontal lines
ax.xaxis.grid() # vertical lines
有什么建议吗?
推荐答案
您可能需要在通话中提供布尔arg,例如使用ax.yaxis.grid(True)
代替ax.yaxis.grid()
.此外,由于您同时使用这两种方法,因此您可以将它们组合到ax.grid
中,这两种方法都可以使用,而不必对每个尺寸都进行一次.
You may need to give boolean arg in your calls, e.g. use ax.yaxis.grid(True)
instead of ax.yaxis.grid()
. Additionally, since you are using both of them you can combine into ax.grid
, which works on both, rather than doing it once for each dimension.
ax = plt.gca()
ax.grid(True)
那应该可以解决您了.
这篇关于使垂直网格线出现在matplotlib中的线图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!