修改熊猫条形图的轴标签

时间:2020-06-04 18:32:15

标签: python pandas plot

我在添加Y刻度时遇到困难。我的总绘图区域包括2个不同的绘图,一个是绘图百分比(df1),另一个不是百分比(df2)。我已经尝试过在Stackoverflow和其他站点上找到的各种解决方案,但是没有任何运气。


import pandas as pd

df1 = pd.DataFrame([80.18, 50.0, 72.3, 90.1, 87.7])
df2 = pd.DataFrame([1000, 2000, 3000, 4000, 5000])



fig, axes = plt.subplots(nrows=1, ncols=2)

df1.plot.bar(ax=axes[0], title = "this", figsize=(8,8), legend=False)

df2.plot.bar(ax=axes[1], title="that", figsize=(8,8), legend=False)

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

对于from matplotlib.ticker import PercentFormatter fig, axes = plt.subplots(nrows=1, ncols=2) subplot = df1.plot.bar(ax=axes[0], title = "this", figsize=(8,8), legend=False) subplot.yaxis.set_major_formatter(PercentFormatter()) df2.plot.bar(ax=axes[1], title="that", figsize=(8,8), legend=False),您可以使用my answer

show timezone;

对于给定的样本数据,将产生:

PercentFormatter [matplotlib-doc]

答案 1 :(得分:0)

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

df1 = pd.DataFrame([80.18, 50.0, 72.3, 90.1, 87.7])
df2 = pd.DataFrame([1000, 2000, 3000, 4000, 5000])

fig, axes = plt.subplots(nrows=1, ncols=2)

ax1 = df1.plot.bar(ax=axes[0], title = "this", figsize=(8,8), legend=False)
vals = ax1.get_yticks()
ax1.set_yticklabels(['{:.0%}'.format(x/100) for x in vals])


ax2 = df2.plot.bar(ax=axes[1], title="that", figsize=(8,8), legend=False)
vals2 = ax2.get_yticks()
ax2.set_yticklabels(['{:.0%}'.format(x/100) for x in vals2])

Picture