一组数据有两个yax记号,两组记号对齐

2024-09-27 19:35:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经设法在右边画出了y轴记号作为百分比,但是标签没有吸引力,我想把它们改成10的倍数。如何更改刻度,使其为10^2%(100%)、10^1%、10^0%等,但仍与左侧的刻度匹配?在

from datetime import datetime
from dateutil.relativedelta import relativedelta
from numpy import nan, arange
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import sys

twentytwo = {}
twentytwo[60.0] = [578, 85, 14, 5, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

totNEA2014 = 1266

def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = str(100 * (y/totNEA2014))
    if matplotlib.rcParams['text.usetex'] == True:
        return s + r'$\%$'
    else:
        return s + '%'

# Plot Details
bar_width = 0.18
line_width = 1
opacity = 1.
centre_bar_positions = arange(20)
zeros = [0 for k in range(len(centre_bar_positions))]

# Make bar plots
fig = plt.figure(figsize=[12, 9])
ax1 = fig.add_subplot(111)
bar_60_22 = plt.bar(centre_bar_positions, twentytwo[60.0], bar_width, bottom=10**-1, alpha=opacity, color='green', log=True, label='V < 22')

plt.title("Some Place")
plt.ylabel("Number of NEAs")
plt.xlabel("Number of apparitions")
ax1.set_ylim([ax1.set_ylim()[0], totNEA2014])
ax1.set_yscale("log")
plt.legend(loc='best')

ax2 = ax1.twinx()
ax2.yaxis.set_label_position("right")
plt.bar(centre_bar_positions, zeros, bar_width, bottom=10**-1, color='white', edgecolor='grey', linewidth=line_width, hatch='0', log=True)
ax2.set_ylim([ax1.set_ylim()[0], ax1.set_ylim()[1]])
formatter = FuncFormatter(to_percent)
plt.gca().yaxis.set_major_formatter(formatter)
plt.ylabel("Percentage of NEAs discovered in 2014")

plt.xlim([.6, 5.8])
plt.show()

这是我的代码目前产生的结果(我减少了上面代码中的条数): enter image description here


Tags: offromimportmatplotlibbarpositionpltwidth
3条回答

像@julienspronck一样,我可能不明白你需要什么/你的阴谋意味着什么,但我同意在哪里修复它。百分比(如果你想用不同的百分比格式来显示的话)在左边),然后

s = str(totNEA2014 * (y/totNEA2014))

产量: enter image description here

我很困惑:左边的yaxis降到了0.1,却被标上了“NEAs的数量”;NEAs的计数真的小于1吗?你愿意有一个左半轴计数,0-批,和一个右半轴是百分比,不从0开始?在

我想我明白了。您希望右轴从0到100%,并且是对数刻度。我想这应该能奏效。在

totNEA2014 = 1266

def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = str(100 * y)
    if matplotlib.rcParams['text.usetex'] == True:
        return s + r'$\%$'
    else:
        return s + '%'

# Plot Details
bar_width = 0.18
line_width = 1
opacity = 1.
centre_bar_positions = arange(20)
zeros = [0 for k in range(len(centre_bar_positions))]

# Make bar plots
fig = plt.figure(figsize=[12, 9])
ax1 = fig.add_subplot(111)
bar_60_22 = plt.bar(centre_bar_positions, twentytwo[60.0], bar_width, bottom=10**-1, alpha=opacity, color='green', log=True, label='V < 22')

plt.title("Some Place")
plt.ylabel("Number of NEAs")
plt.xlabel("Number of apparitions")
ax1.set_ylim([ax1.set_ylim()[0], totNEA2014])
ax1.set_yscale("log")
plt.legend(loc='best')

ax2 = ax1.twinx()
ax2.yaxis.set_label_position("right")
ax2.set_yscale("log")
ax1.bar(centre_bar_positions, zeros, bar_width, bottom=10**-1, color='white', edgecolor='grey', linewidth=line_width, hatch='0', log=True)
ax2.set_ylim([0.0001, 1])
ax2.set_yticks([0.0001, 0.001, 0.01, 0.1, 1.])
formatter = FuncFormatter(to_percent)
ax2.get_yaxis().set_major_formatter(formatter)
plt.ylabel("Percentage of NEAs discovered in 2014")

plt.xlim([.6, 5.8])
plt.show()

请注意,我更改了格式化程序函数和打印线(现在只打印到ax1,而不是两个轴)

使用.add_artist(matplotlib.axis.YAxis(ax1))添加额外的轴比使用.twinx()更健壮。在

当您只需要一个额外的时,用.twinx()添加一个extras对象似乎有些过头了。使用问题中提出的.twinx()也有一个缺点,即第二个轴与数据分离,因此您需要一些技巧使其与数据和另一个轴相对应,例如更改ylim(在其他答案中以各种方式完成),但是,一旦您更改原始轴上的ylim,就会中断。在

见下文。我更改了to_percent函数中的第一行,以获得非常好的数字格式和名为“addsecondy-AXIS”的块。在

from datetime import datetime
from dateutil.relativedelta import relativedelta
from numpy import nan, arange
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import sys

twentytwo = {}
twentytwo[60.0] = [578, 85, 14, 5, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

totNEA2014 = 1266

def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = '%g' % (100 * (y/totNEA2014))
    if matplotlib.rcParams['text.usetex'] == True:
        return s + r'$\%$'
    else:
        return s + '%'

# Plot Details
bar_width = 0.18
line_width = 1
opacity = 1.
centre_bar_positions = arange(20)
zeros = [0 for k in range(len(centre_bar_positions))]

# Make bar plots
fig = plt.figure(figsize=[12, 9])
ax1 = fig.add_subplot(111)
bar_60_22 = plt.bar(centre_bar_positions, twentytwo[60.0], bar_width, bottom=10**-1, alpha=opacity, color='green', log=True, label='V < 22')

plt.title("Some Place")
plt.ylabel("Number of NEAs")
plt.xlabel("Number of apparitions")
ax1.set_ylim([ax1.set_ylim()[0], totNEA2014])
ax1.set_yscale("log")
plt.legend(loc='best')

# ADD SECOND Y-AXIS    
extra_axis = matplotlib.axis.YAxis(ax1)
extra_axis.tick_right()
formatter = FuncFormatter(to_percent)
extra_axis.set_major_formatter(formatter)
extra_axis.set_ticks([totNEA2014*10**p for p in range(-4, 1)])
extra_axis.set_label_text("Percentage of NEAs discovered in 2014")
extra_axis.set_label_position('right')
ax1.add_artist(extra_axis)

plt.xlim([.6, 5.8])
plt.show()

相关问题 更多 >

    热门问题