我正在尝试定义一个 matplotlib 样式文件 (.mplstyle) 以仅显示水平网格线。我知道我可以在 python 中使用
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
仅此而已。
这是结果:
最佳答案
你还需要打开网格,
import matplotlib.pyplot as plt
plt.rcParams["axes.grid.axis"] ="y"
plt.rcParams["axes.grid"] = True
或者在matplotlibrc文件中:
axes.grid.axis : y
axes.grid : True
关于Python Matplotlib 样式文件 : show only horizontal gridlines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46457615/