我已设法在右侧绘制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()
这是我的代码当前生成的内容(我减少了上面代码中的条数):
答案 0 :(得分:2)
我想我明白了。您希望右轴从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而不是两个轴)
答案 1 :(得分:0)
就像@ julien-spronck一样,我可能不理解你需要什么/你的情节意味着什么,但我同意我会在哪里解决它。如果您只想以不同的格式显示相同的数字(右侧的百分比,左侧的百分比(?)的对数比例),那么
s = str(totNEA2014 * (y/totNEA2014))
收率:
我很疑惑:左手yaxis下降到0.1,但被“NEAs数量”标记; NEAs真的算不上一个吗?你宁愿左手yaxis有计数,0手数,右手yaxis是百分比而不是从0开始?答案 2 :(得分:0)
使用.add_artist(matplotlib.axis.YAxis(ax1))
添加额外的轴比使用.twinx()
更强大。
使用.twinx()
添加额外的 Axes 对象似乎有点过分,只需要一个额外的轴。使用问题中提出的.twinx()
也有一个缺点,即第二个轴与数据分离,因此您需要一些黑客使其与数据和另一个轴相对应,例如更改ylim
(在其他答案中以各种方式完成) - 但是,一旦在原始轴上更改ylim
,这将会中断。
见下文。我更改了to_percent
函数中的第一行以获得漂亮的数字格式,并将块调用为&#34;添加第二个Y轴&#34;。
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()