Python 3.x 设置Ytick格式,以10为单位显示子批次中每个子批次的百分比(0-100%)
我正在尝试将Python 3.x 设置Ytick格式,以10为单位显示子批次中每个子批次的百分比(0-100%),python-3.x,matplotlib,subplot,ticker,Python 3.x,Matplotlib,Subplot,Ticker,我正在尝试将子绘图图像中的Ytick格式化为与单绘图图像中的Ytick相似。我使用的代码适用于单绘图,但不适用于子绘图 以下是绘图和样本数据的代码 x = ['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06'] final_df['abandonment_rate(%)'] = [70.00, 78.25, 15.25, 53.78, 62.75, 11.00] final_df['booking_rate(%)'
子绘图
图像中的Ytick格式化为与单绘图
图像中的Ytick相似。我使用的代码适用于单绘图
,但不适用于子绘图
以下是绘图和样本数据的代码
x = ['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06']
final_df['abandonment_rate(%)'] = [70.00, 78.25, 15.25, 53.78, 62.75, 11.00]
final_df['booking_rate(%)'] = [50.00, 28.25, 35.25, 33.78, 12.75, 21.00]
单点绘图代码
此代码中的yaxis格式生成所需的ytick标签
x = [i for i in checkins_df_entire_apt['month_yr'].apply(lambda x: x.strftime('%Y-%m'))]
x_indexes = np.arange(len(x))
width = 0.25
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
rects1 = ax.bar(x_indexes + 0.25,
final_df['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
rects2 = ax.bar(x_indexes + 0.5,
final_df['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.legend(("Abandonment Rate (%)", "Conversion Rate (%)"), fontsize=25)
plt.xticks(ticks=x_indexes + 1.5*width, labels=x, fontsize=20)
plt.yticks(fontsize=20)
plt.title("As percentage of Interaction Started", fontsize=30, ha='center')
plt.suptitle("Overall conversion/abandonment rate - Entire Apartment", fontsize=40, ha='center')
plt.xlabel("Month-yr", fontsize=40)
plt.ylabel("Conversion Rate (%)", fontsize=40)
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{} %'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(18, 15), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=20)
autolabel(rects1)
autolabel(rects2)
plt.show()
子批次代码
但是由于某些原因,下面代码中的格式设置不起作用
x = [i for i in checkins_df_private_room['month_yr'].apply(lambda x: x.strftime('%Y-%m'))]
x_indexes = np.arange(len(x))
width = 0.20
fig, ax = plt.subplots(3, 1, sharex=True)
top_ax, middle_ax, bottom_ax = ax
# Private Room
plt.subplot(3, 1, 1)
top_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
plt.bar(x_indexes + 0.00,
checkins_df_private_room['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
plt.bar(x_indexes + 0.20,
checkins_df_private_room['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
plt.legend(loc='upper right', fontsize=30)
plt.yticks(fontsize=20)
plt.ylabel("Private Room", fontsize=30)
# Entire Apartment
plt.subplot(3, 1, 2)
middle_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
plt.bar(x_indexes + 0.00,
checkins_df_entire_apt['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
plt.bar(x_indexes + 0.20,
checkins_df_entire_apt['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
plt.yticks(fontsize=20)
plt.ylabel("Entire Apartment", fontsize=30)
# Shared Room
plt.subplot(3, 1, 3)
bottom_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
plt.bar(x_indexes + 0.00,
checkins_df_shared_room['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
plt.bar(x_indexes + 0.20,
checkins_df_shared_room['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
plt.suptitle("Overall conversion/abandonment rate - Apartment Type", fontsize=40, ha='center')
plt.xlabel("Month-yr", fontsize=40)
plt.yticks(fontsize=20)
plt.ylabel("Shared Room", fontsize=30)
plt.show()
解决方案
旁注,但为什么不直接使用?我尝试了
top\u ax.yaxis.set\u major\u格式化程序(mtick.PercentFormatter(xmax=100,symbol='%'))
,但它不起作用!我还使用plt.subplot(3,1,1)
分别标记每个子批。不确定是否有更好的方法。标记要混合的y轴。你需要始终如一。嘿,谢谢你指出这一点!成功了!
x = [i for i in checkins_df_private_room['month_yr'].apply(lambda x: x.strftime('%Y-%m'))]
x_indexes = np.arange(len(x))
width = 0.20
fig, ax = plt.subplots(3, 1, sharex=True)
top_ax, middle_ax, bottom_ax = ax
# Private Room
plt.subplot(3, 1, 1)
top_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
plt.bar(x_indexes + 0.00,
checkins_df_private_room['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
plt.bar(x_indexes + 0.20,
checkins_df_private_room['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
plt.legend(loc='upper right', fontsize=30)
plt.yticks(fontsize=20)
plt.ylabel("Private Room", fontsize=30)
# Entire Apartment
plt.subplot(3, 1, 2)
middle_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
plt.bar(x_indexes + 0.00,
checkins_df_entire_apt['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
plt.bar(x_indexes + 0.20,
checkins_df_entire_apt['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
plt.yticks(fontsize=20)
plt.ylabel("Entire Apartment", fontsize=30)
# Shared Room
plt.subplot(3, 1, 3)
bottom_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
plt.bar(x_indexes + 0.00,
checkins_df_shared_room['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
plt.bar(x_indexes + 0.20,
checkins_df_shared_room['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
plt.suptitle("Overall conversion/abandonment rate - Apartment Type", fontsize=40, ha='center')
plt.xlabel("Month-yr", fontsize=40)
plt.yticks(fontsize=20)
plt.ylabel("Shared Room", fontsize=30)
plt.show()
x = [i for i in checkins_df_private_room['month_yr'].apply(lambda x: x.strftime('%Y-%m'))]
x_indexes = np.arange(len(x))
y_indexes = np.arange(0, 110, 10)
width = 0.20
fig, ax = plt.subplots(3,1)
top_ax, middle_ax, bottom_ax = ax
################
# Private Room #
################
top_ax.bar(x_indexes + 0.00,
checkins_df_private_room['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
top_ax.bar(x_indexes + 0.20,
checkins_df_private_room['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
# plt.xticks(ticks=x_indexes + 0.5*width, labels=x, fontsize=20)
top_ax.legend(fontsize=30, loc='upper right')
top_ax.set_title('(Private Room)', fontsize=30)
top_ax.set_xticks(ticks=x_indexes + 0.5*width)
top_ax.set_xticklabels(labels=x, fontsize=30)
top_ax.set_yticks(ticks=y_indexes)
top_ax.set_yticklabels(y_indexes, fontsize = 30)
####################
# Entire Apartment #
####################
middle_ax.bar(x_indexes + 0.00,
checkins_df_entire_apt['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
middle_ax.bar(x_indexes + 0.20,
checkins_df_entire_apt['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
middle_ax.set_xticks(ticks=x_indexes + 0.5*width)
middle_ax.set_xticklabels(labels=x, fontsize=30)
middle_ax.set_yticks(ticks=y_indexes)
middle_ax.set_yticklabels(y_indexes, fontsize = 30)
middle_ax.set_ylabel('As Percentage of Interaction Started (%)',
fontsize=40)
middle_ax.set_title('(Entire Apartment)', fontsize=30)
###############
# Shared Room #
###############
bottom_ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
bottom_ax.bar(x_indexes + 0.00,
checkins_df_shared_room['abandonment_rate(%)'],
width=width,
color="#484848",
label="Abandonment Rate (%)")
bottom_ax.bar(x_indexes + 0.20,
checkins_df_shared_room['booking_rate(%)'],
width=width,
color="#00A699",
label="Booking Rate (%)")
bottom_ax.set_xticks(ticks=x_indexes + 0.5*width)
bottom_ax.set_xticklabels(labels=x, fontsize=30)
bottom_ax.set_yticks(ticks=y_indexes)
bottom_ax.set_yticklabels(y_indexes, fontsize=30)
bottom_ax.set_xlabel('Month-Yr', fontsize=40)
bottom_ax.set_title('(Shared Room)', fontsize=30)
plt.suptitle("Overall conversion/abandonment rate - Apartment Type", fontsize=50, ha='center')
plt.show()