2017-09-27 120 views
1

我想要定义一个matplotlib样式文件(.mplstyle)以仅显示水平网格线。我知道我可以用python做它Python Matplotlib样式文件:仅显示水平网格线

fig, ax = plt.subplots() 
ax.yaxis.grid(True) 

但我想在mplstyle文件中做到这一点。 classic.mplstyle文件具有选项axes.grid.axis: both集。我试图改变这axes.grid.axis: y,但这似乎并没有改变任何东西。

是否可以在样式文件中做到这一点?

编辑:

在得到这个工作的尝试:

%matplotlib inline 
import pandas as pd 
import matplotlib.pyplot as plt 
month = ['Jan', 'Feb', 'Mar', 'Apr'] 
volume = [1, 2, 3, 4] 
plt.style.use('https://gist.githubusercontent.com/luisdelatorre012/b36899e6dca07d05e73aca80eceb3098/raw/43ae73605b5e33dfc3f0d7e5d423ff997fc8325c/tiny.mplstyle') 
d = pd.DataFrame({'Month': month, 'Volume': volume}) 
fig, ax = plt.subplots() 
b = d.plot(kind='bar', y="Volume", x="Month", ax=ax) 
plt.title('Title') 
plt.show() 

文件tiny.mplstyle包含

axes.grid.axis: y 
axes.grid: True 

,别无其他。

这是结果:

gridlines

+0

你重新启动Matplotlib? – Joe

+0

@Joe,我在jupyter笔记本中测试了这个,并在重新运行之前重新启动内核,以便多个matplotlib样式文件不会一起应用。这是你指的是什么? – AnonymousCowherd

回答

1

您需要打开网格上为好,

import matplotlib.pyplot as plt 
plt.rcParams["axes.grid.axis"] ="y" 
plt.rcParams["axes.grid"] = True 

或matplotlibrc文件:

axes.grid.axis : y 
axes.grid : True 
+0

谢谢。到目前为止,我已经尝试过两件事情,两者都会显示水平和垂直线条。 1)使用一个.mplstyle文件,其中只包含行的坐标轴.g.g.axis:y和axes.grid:True,将axes.grid.axis更改为y,并将axes.grid更改为ggplot.mplstyle中的True 。 – AnonymousCowherd

+0

您使用的是哪个版本的matplotlib? – ImportanceOfBeingErnest

+0

matplotlib 2.0.2 – AnonymousCowherd