Python Matplotlib 样式文件 : show only horizontal gridlines

标签 python matplotlib

我正在尝试定义一个 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

仅此而已。

这是结果:

gridlines

最佳答案

你还需要打开网格,

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/

相关文章:

python - 如何使用 seaborn 为我的 DataFrame 创建堆叠条形图

python - 如何在 Matplotlib 中设置小于 1 的对数刻度

python - 如何设置为默认 matplotlib 等高线图以始终标记等高线

python - 暂停 matplotlib 自定义动画循环

python - 如何在 Seaborn 的分组箱线图中添加垂直网格线?

python - Ipython %matplotlib 给出 "ImportError: No module named moves"尽管通过 pip 安装了移动

python - 追加列 pandas : TypeError: assign() takes 1 positional argument but 2 were given

python - 为 joyplot 设置 x 轴标签

python - 使用命令行参数打开 Python 文件

python - FastAPI 的 RedirectResponse 在 Swagger UI 中无法按预期工作