Matplotlib,全局设置刻度数.X 轴、Y 轴、颜色条 [英] Matplotlib, globally set number of ticks. X-axis, Y-axis, colorbar
问题描述
对于我喜欢使用的字体大小,我发现 5 个刻度是在 matplotlib 中几乎每个轴上视觉上最令人愉悦的刻度数.我也喜欢修剪沿 x 轴的最小刻度以避免重叠刻度标签.因此,对于几乎每个情节,我都使用以下代码进行查找.
For the font size I like to use I've found that 5 ticks is the most visually pleasing number of ticks across pretty much every axis in matplotlib. I also like to prune the smallest tick along the x-axis to avoid overlapping tick lables. So for almost every plot I make I find myself using the following code.
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
plt.imshow( np.random.random(100,100) )
plt.gca().xaxis.set_major_locator( MaxNLocator(nbins = 7, prune = 'lower') )
plt.gca().yaxis.set_major_locator( MaxNLocator(nbins = 6) )
cbar = plt.colorbar()
cbar.locator = MaxNLocator( nbins = 6)
plt.show()
是否有我可以使用的 rc 设置,以便我的 x 轴、y 轴和颜色条的默认定位器默认是上述 MaxNLocator,在 x 轴上带有修剪选项?
Is there an rc setting I can use so that the default locator for my x-axis, y-axis, and colorbar is by default the above MaxNLocator with the prune option on the x axis?
推荐答案
为什么不编写一个自定义模块 myplotlib
来设置这些默认值?
Why don't you just write a custom module myplotlib
that sets these defaults as you like them?
import myplt
myplt.setmydefaults()
全局rc设置可能会破坏依赖于这些设置而不进行修改的其他应用程序.
A global rc setting might break other applications that rely on these settings to be not modified.
这篇关于Matplotlib,全局设置刻度数.X 轴、Y 轴、颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!