[英]Python Pandas Plotting Two BARH side by side
左侧图表(带有图例的图)源自df_2
,右侧图表源自df_1
。
但是,我不能让两个并排的图共享 y 轴。
这是我目前的绘图方式:
df_1[target_cols].plot(kind='barh', x='LABEL', stacked=True, legend=False)
df_2[target_cols].plot(kind='barh', x='LABEL', stacked=True).invert_xaxis()
plt.show()
该代码将在两个不同的“画布”中生成两个图。
df_2
图表)的 y 轴标签? 您可以使用plt.subplots(sharey=True)
创建共享子图。 然后将数据帧绘制到两个子图。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
a = np.random.randint(5,15, size=10)
b = np.random.randint(5,15, size=10)
df = pd.DataFrame({"a":a})
df2 = pd.DataFrame({"b":b})
fig, (ax, ax2) = plt.subplots(ncols=2, sharey=True)
ax.invert_xaxis()
ax.yaxis.tick_right()
df["a"].plot(kind='barh', x='LABEL', legend=False, ax=ax)
df2["b"].plot(kind='barh', x='LABEL',ax=ax2)
plt.show()
起初是用
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0], axes[1]
但随后无法将 y-ticks 放在右侧(因此它们看起来居中),由于某种原因不起作用。 现在使用 'ax' 和 ' ax.yaxis.tick_right()
' 和ax.yaxis.tick_right()
工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.