python - 如何在 matplotlib 配置文件中定义次要刻度数

标签 python matplotlib

使用一个 Locator()可以控制 matplotlib 轴上的次要或主要刻度数。

例如 :

plt.gca().yaxis.set_minor_locator(plt.MultipleLocator(1))

但是上面的行取决于您正在绘制的数据。

我想知道是否有办法从 matplotlib 样式文件(例如 matplotlibrc)中更改次要刻度数。 ( https://matplotlib.org/users/customizing.html )。例如,您可以通过以下方式管理小刻度样式:
xtick.minor.visible : True
xtick.minor.width   : 2
xtick.minor.size    : 5

但我不知道如何处理次要滴答声。

最佳答案

刻度数实际上并不被视为“样式”属性。因此,您目前无法选择为此使用 rc 文件。
至于默认设置的来源,这是由 AutoMinorLocator 决定的。 ,如果没有给出其他自定义定位器并且打开了次要刻度,则使用它。

matplotlib.ticker.AutoMinorLocator(n=None)

Dynamically find minor tick positions based on the positions of major ticks. The scale must be linear with major ticks evenly spaced.

n is the number of subdivisions of the interval between major ticks; e.g., n=2 will place a single minor tick midway between major ticks.

If n is omitted or None, it will be set to 5 or 4.


这为以下解决方法留下了空间。你可以给 AutoMinorLocator 打补丁使用与平常不同的默认值。例如。有 23 个小垃圾箱,
# use these lines on top of your matplotlib script
import matplotlib.ticker
class MyLocator(matplotlib.ticker.AutoMinorLocator):
    def __init__(self, n=23):
        super().__init__(n=n)
matplotlib.ticker.AutoMinorLocator = MyLocator        
 

# Now use matplotlib as usual.       
import matplotlib.pyplot as plt
plt.rcParams["xtick.minor.visible"] =  True

plt.plot([1,2])
plt.show()

关于python - 如何在 matplotlib 配置文件中定义次要刻度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53634709/

相关文章:

python - 如何在 Python Pandas Dataframe 中有条件地连接 2 列

python - 如何在条形图上隔开 x 刻度并缩放 y 轴

python - 从数组编写动画散点图

python - 当 x 值之间发生单击时,在固定 x 值上绘制 h 线

python - 将遥测数据添加到视频 [python]

Python 3.2 - 曲奇库

python - 如何使用 Python 3 通过 C API 定义带有方法的新类?

python - AIML - 将用户输入解析为 Python 变量

python - Matplotlib 在 Spyder ipython 中卡住

python - 使用 matplotlib 绘制“circos”风格的图?