为 Matplotlib 创建默认值集
我经常为自己的研究制作绘图,所有默认设置都很好,但经常必须切换到为演讲/演示设计绘图;我手动设置所有字体大小 大一点以便于阅读:
plot(xdata, ydata)
xlabel("x-axis data", fontsize=20)
ax = gca()
for labeltick in ax.xaxis.get_majorticklabels() + ax.yaxis.get_majorticklabels():
labeltick.set_fontsize(15)
等等。
感谢诸如这个之类的文档和问题,我知道如何当我启动 matplotlib 时控制默认绘图参数。我想写一些非常快的东西(mpl_defaults.py):
import matplotlib as mpl
def plot_for_talks():
mpl.rcParams['font.size'] = 20
mpl.rcParams['figure.subplot.left'] = .2
mpl.rcParams['figure.subplot.right'] = .8
mpl.rcParams['figure.subplot.bottom'] = .2
mpl.rcParams['figure.subplot.top'] = .8
然后我的绘图代码可以只包括
import mpl_defaults
plot_for_talks()
我的问题:有没有更合适的方法来做到这一点?也许已经内置了一些东西?
I am frequently making plots for my own research and all of the default settings are fine, but often have to switch over to making plots designed for talks/presentations; I manually set all of the font sizes a bit bigger for easier reading:
plot(xdata, ydata)
xlabel("x-axis data", fontsize=20)
ax = gca()
for labeltick in ax.xaxis.get_majorticklabels() + ax.yaxis.get_majorticklabels():
labeltick.set_fontsize(15)
and so on.
Thanks to documentation and questions like this one I know how to control default plotting parameters when I start up matplotlib. I thought about writing something really quick (mpl_defaults.py):
import matplotlib as mpl
def plot_for_talks():
mpl.rcParams['font.size'] = 20
mpl.rcParams['figure.subplot.left'] = .2
mpl.rcParams['figure.subplot.right'] = .8
mpl.rcParams['figure.subplot.bottom'] = .2
mpl.rcParams['figure.subplot.top'] = .8
Then my plotting code could just include
import mpl_defaults
plot_for_talks()
My question: is there a more appropriate way to do this? Maybe something already built in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您按目录管理单独的演示模式,则可以在每个项目目录中放置一个 matplotlibrc 文件,并 matplotlib将使用当前目录中的目录。
If you manage your separate presentation modes by directories, you can put a matplotlibrc file in each project directory, and matplotlib will use the one in the current directory.
试试这个:
应该有一个“site-packages/matplotlib/mpl-data/matplotlibrc”文件,如文档 5.1 中所述。
使用 mpl.matplotlib_fname() 获取 rc 文件路径,并修改它以使设置永久有效。
Try this:
And there should be a "site-packages/matplotlib/mpl-data/matplotlibrc" file, described in doc 5.1.
Use mpl.matplotlib_fname() to get your rc file path, and modify it so the setting will be permanent.