【发布时间】:2021-12-14 08:45:59
【问题描述】:
我有两个关于推文情绪的工作条形图(中性、积极、消极)。如何将它们并排合并为一个?
第一小节:
plt.figure(figsize=(6,5))
plt.title('Classification of All tweets into sentiment categories',fontsize=15)
plt.ylabel('Percentage [%]',fontsize=18)
ax = (df_navalny.sentiment.value_counts()/len(df_navalny)*100).plot(kind="bar", rot=0,color=['#04407F','#0656AC','#0A73E1'])
ax.set_yticks(np.arange(0, 110, 10))
plt.grid(color='#95a5a6', linestyle='-.', linewidth=1, axis='y', alpha=0.7)
ax2 = ax.twinx()
ax2.set_yticks(np.arange(0, 110, 10)*len(df_navalny)/100)
for p in ax.patches:
ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
第二栏:
plt.figure(figsize=(6,5))
plt.title('Classification of All tweets into sentiment categories',fontsize=15)
plt.ylabel('Percentage [%]',fontsize=18)
ax = (df_putin.sentiment.value_counts()/len(df_putin)*100).plot(kind="bar", rot=0,color=['#04407F','#0656AC','#0A73E1'])
ax.set_yticks(np.arange(0, 110, 10))
plt.grid(color='#95a5a6', linestyle='-.', linewidth=1, axis='y', alpha=0.7)
ax2 = ax.twinx()
ax2.set_yticks(np.arange(0, 110, 10)*len(df_putin)/100)
for p in ax.patches:
ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
【问题讨论】:
-
您尝试过使用子图吗? matplotlib.org/stable/api/_as_gen/…
-
@pask 作为带有标签的分组条形图,是的,但我无法在此示例中使用我的代码
-
fig, (ax1, ax2) = plt.subplots(1, 2) 用 ax1 定义第一个图,用 ax2 定义第二个图。
-
如果没有数据,这个问题就无法重现。这个问题需要SSCCE。请参阅How to provide a reproducible dataframe,然后edit 您的问题,然后将剪贴板粘贴到代码块中。始终提供minimal reproducible example代码、数据、错误、当前输出和预期输出,如formatted text。如果相关,绘图图像是可以的。如果您不包含 mre,则该问题可能会被否决、关闭和删除。
标签: python pandas dataframe matplotlib