Python Matplotlib样式文件:仅显示水平网格线 [英] Python Matplotlib style file: show only horizontal gridlines
本文介绍了Python Matplotlib样式文件:仅显示水平网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试定义一个 matplotlib 样式文件 (.mplstyle) 以仅显示水平网格线.我知道我可以使用
在python中完成此操作fig, ax = plt.subplots()ax.yaxis.grid(真)
但我想在 mplstyle 文件中进行.
解决方案
你也需要打开网格,
将matplotlib.pyplot导入为pltplt.rcParams["axes.grid.axis"] ="y"plt.rcParams ["axes.grid"] = True
或在 matplotlibrc 文件中:
axes.grid.axis : yaxis.grid : 真
I'm trying to define a matplotlib style file (.mplstyle) to only show horizontal gridlines. I know I can do it in python with
fig, ax = plt.subplots()
ax.yaxis.grid(True)
but I want to do it in the mplstyle file. The classic.mplstyle file has the option axes.grid.axis: both
set. I tried changing this to axes.grid.axis: y
, but this doesn't seem to change anything.
Is it possible to do this in a style file?
EDIT:
An attempt at getting this to work:
%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()
The file tiny.mplstyle contains
axes.grid.axis: y
axes.grid: True
and nothing else.
This is the result:
解决方案
You need to turn the grid on as well,
import matplotlib.pyplot as plt
plt.rcParams["axes.grid.axis"] ="y"
plt.rcParams["axes.grid"] = True
or in the matplotlibrc file:
axes.grid.axis : y
axes.grid : True
这篇关于Python Matplotlib样式文件:仅显示水平网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文