Python 使垂直网格线出现在 matplotlib 的线图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16074392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting vertical gridlines to appear in line plot in matplotlib
提问by Osmond Bishop
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.DataFramefrom 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.
我想在我的绘图上同时显示水平和垂直网格线,但默认情况下只显示水平网格线。我pandas.DataFrame在 python 中使用来自 sql 查询的 a 生成一个在 x 轴上带有日期的线图。我不知道为什么它们没有出现在日期上,我试图寻找答案,但找不到。
All I have used to plot the graph is the simple code below.
我用来绘制图形的只是下面的简单代码。
data.plot()
grid('on')
data is the DataFrame which contains the dates and the data from the sql query.
data 是包含日期和来自 sql 查询的数据的 DataFrame。
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
Any suggestions?
有什么建议?


采纳答案by wim
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.
您可能需要在调用中提供 boolean arg,例如使用ax.yaxis.grid(True)而不是ax.yaxis.grid(). 此外,由于您同时使用它们,因此您可以组合到 中ax.grid,这对两者都适用,而不是对每个维度都执行一次。
ax = plt.gca()
ax.grid(True)
That should sort you out.
那应该可以解决您的问题。
回答by matt
plt.gca().xaxis.grid(True)proved to be the solution for me
plt.gca().xaxis.grid(True)事实证明是我的解决方案
回答by Lory_yang
maybe this can solve the problem: matplotlib, define size of a grid on a plot
也许这可以解决问题: matplotlib,在绘图上定义网格的大小
ax.grid(True, which='both')
The truth is that the grid is working, but there's only one v-grid in 00:00 and no grid in others. I meet the same problem that there's only one grid in Nov 1 among many days.
事实是网格正在工作,但在 00:00 只有一个 v-grid,其他没有网格。我遇到了同样的问题,在许多天中,11 月 1 日只有一个网格。
回答by jdhao
According to matplotlib documentation, The signature of the Axesclass grid()method is as follows:
根据matplotlib文档,Axes类grid()方法的签名如下:
Axes.grid(b=None, which='major', axis='both', **kwargs)
Turn the axes grids on or off.
whichcan be ‘major' (default), ‘minor', or ‘both' to control whether major tick grids, minor tick grids, or both are affected.
axiscan be ‘both' (default), ‘x', or ‘y' to control which set of gridlines are drawn.
Axes.grid(b=None, which='major', axis='both', **kwargs)
打开或关闭轴网格。
which可以是“主要”(默认)、“次要”或“两者”,以控制主要刻度网格、次要刻度网格或两者都受到影响。
axis可以是“both”(默认)、“x”或“y”来控制绘制哪一组网格线。
So in order to show grid lines for both the x axis and y axis, we can use the the following code:
因此,为了同时显示 x 轴和 y 轴的网格线,我们可以使用以下代码:
ax = plt.gca()
ax.grid(which='major', axis='both', linestyle='--')
This method gives us finer control over what to show for grid lines.
这种方法使我们可以更好地控制网格线显示的内容。
回答by Mudit
For only horizontal lines
仅用于水平线
ax = plt.axes()
ax.yaxis.grid() # horizontal lines
This worked
这有效
回答by Dark
Short answer (read below for more info):
简短回答(阅读下文了解更多信息):
ax.grid(axis='both', which='both')
What you do is correct and it should work.
你所做的是正确的,它应该可以工作。
However, since the Xaxis in your example is a DateTimeaxis the Majortick-marks (most probably) are appearing only at the both ends of the X axis. The other visible tick-marks are Minortick-marks.
但是,由于您示例中的X轴是DateTime轴,因此主要刻度线(最有可能)仅出现在 X 轴的两端。其他可见的刻度线是次要刻度线。
The ax.grid()method, by default, draws grid lines on Major tick-marks.
Therefore, nothing appears in your plot.
ax.grid()默认情况下,该方法在主要刻度线上绘制网格线。因此,您的情节中没有任何内容。
Use the code below to highlight the tick-marks. Majors will be Blue while Minors are Red.
使用下面的代码突出显示刻度线。Majors 为蓝色,Minor 为红色。
ax.tick_params(which='both', width=3)
ax.tick_params(which='major', length=20, color='b')
ax.tick_params(which='minor', length=10, color='r')
Now to force the grid lines to be appear also on the Minor tick-marks, pass the which='minor'to the method:
现在要强制网格线也出现在次要刻度线上,请将 传递which='minor'给方法:
ax.grid(b=True, which='minor', axis='x', color='#000000', linestyle='--')
or simply use which='both'to draw both Major and Minor grid lines.
And this a more elegant grid line:
或简单地用于which='both'绘制主要和次要网格线。这是一条更优雅的网格线:
ax.grid(b=True, which='minor', axis='both', color='#888888', linestyle='--')
ax.grid(b=True, which='major', axis='both', color='#000000', linestyle='-')

