Matplotlib小滴答声 [英] Matplotlib minor ticks

查看:45
本文介绍了Matplotlib小滴答声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义matplotlib图中的次要刻度线.考虑以下代码:

导入pylab为pl从matplotlib.ticker导入AutoMinorLocator无花果,ax = pl.subplots(figsize =(11.,7.4))x = [1,2,3,4]y = [10,45,77,55]错误b = [20,66,58,11]pl.xscale("日志")ax.xaxis.set_minor_locator(AutoMinorLocator(2))ax.yaxis.set_minor_locator(AutoMinorLocator(2))pl.tick_params(which='both', width=1)pl.tick_params(which='minor', length=4, color='g')pl.tick_params(axis ='both', which='major', length=8, labelsize =20, color='r' )pl.errorbar(x,y,yerr = errorb)#pl.plot(x, y)pl.show()

据我了解, AutoMinorLocator(n)应该在每个主要刻度之间插入n个次刻度,这是线性发生的事情,但根本无法弄清楚背后的逻辑在对数刻度上放置次要刻度.最重要的是,使用 errorbar() 比使用简单的 plot() 时有更多的小刻度.

解决方案

AutoMinorLocator仅设计用于线性比例尺:

来自

I am trying to customize the minor ticks in a matplotlib plot. Consider the following code:

import pylab as pl
from matplotlib.ticker import AutoMinorLocator

fig, ax = pl.subplots(figsize=(11., 7.4))

x = [1,2,3, 4]
y = [10, 45, 77, 55]
errorb = [20,66,58,11]

pl.xscale("log")

ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

pl.tick_params(which='both', width=1)
pl.tick_params(which='minor', length=4, color='g')
pl.tick_params(axis ='both', which='major', length=8, labelsize =20, color='r' )

pl.errorbar(x, y, yerr=errorb)
#pl.plot(x, y)

pl.show()

As far as I understood, AutoMinorLocator(n) is supposed to insert n minor ticks between each major tick, and this is what happens on a linear scale but simply cannot figure out the logic behind the placement of the minor ticks on a logscale. On the top of that, there are much more minor ticks when using errorbar() then when using the simple plot().

解决方案

AutoMinorLocator is only designed to work for linear scales:

From the ticker documentation:

AutoMinorLocator

locator for minor ticks when the axis is linear and the major ticks are uniformly spaced. It subdivides the major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the major interval.

And the AutoMinorLocator documentation:

Dynamically find minor tick positions based on the positions of major ticks. Assumes the scale is linear and major ticks are evenly spaced.

You probably want to use the LogLocator for your purposes.

For example, to put major ticks in base 10, and minor ticks at 2 and 5 on your plot (or every base*i*[2,5]), you could:

ax.xaxis.set_major_locator(LogLocator(base=10))
ax.xaxis.set_minor_locator(LogLocator(base=10,subs=[2.0,5.0]))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

这篇关于Matplotlib小滴答声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆