Python Pandas并排绘制两个BARH [英] Python Pandas Plotting Two BARH side by side
本文介绍了Python Pandas并排绘制两个BARH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制类似这样的情节,
I am trying to make a plot that is similar like this,
左侧图表(带有图例的图)源自df_2
,右侧图表源自df_1
.
The left hand side chart (the plot with legend) is derived from df_2
, and the right hand side chart is derived from df_1
.
但是,我不能使两个图并排共享y轴.
However, I cannot make the two plots side-by-side share the y-axis.
这是我当前的绘制方式:
Here is my current way to plot:
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()
代码将在两个不同的画布"中生成两个图.
The code will resulted two plots in two different "canvas".
- 如何使它们并排共享y轴?
- 如何删除左侧图表的y轴标签(来自
df_2
的图表)?
- How can I make them side-by-side sharing the y-axis?
- How can I remove the label in y-axis for the left hand side chart (chart derived from
df_2
)?
任何建议将不胜感激.谢谢.
Any suggestions will be much appreciated. Thanks.
推荐答案
您可以使用plt.subplots(sharey=True)
创建共享子图.然后将数据框绘制到两个子图上.
You can create shared subplots using plt.subplots(sharey=True)
. Then plot the dataframes to the two subplots.
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()
这篇关于Python Pandas并排绘制两个BARH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文