无法为堆积条形图设置标签

时间:2021-02-03 18:15:59

标签: python plot

我正在尝试为 y 轴和辅助 y 轴设置标签,但是,我无法为 y 轴设置标签,非常感谢帮助。使用此代码 plt.ylabel("data (MB)") 为辅助 y 轴设置标签,我需要 x、y 和辅助 y 轴的标签。谢谢

 m1_t = pd.DataFrame({
 'device1': [13.22,43.20,146.38],
 'device2':[609.52,663.49,591.76],
 'comm':[230,5010,5010],
 'data':[12.545,12.545,3.137
  })
 colors = ['#5DADE2', '#48C9B0', '#D5DBDB']
 m1_t[['device1','device2','comm']].plot(kind='bar', width = 
 width,stacked=True,color=colors)
 m1_t['data'].plot(secondary_y=True,color='darkslategrey',marker='o',MarkerSize= 3)
 ax = plt.gca()
 plt.xlim([-width, len(m1_t['comm'])-width])
 ax.set_xticklabels(("1", "2", "3"))
 plt.ylabel("data (MB)")

1 个答案:

答案 0 :(得分:1)

在绘制第二个图之前放置一个 plt.ylabel('Text')

m1_t = pd.DataFrame({
 'device1': [13.22,43.20,146.38],
 'device2':[609.52,663.49,591.76],
 'comm':[230,5010,5010],
 'data':[12.545,12.545,3.137]})
colors = ['#5DADE2', '#48C9B0', '#D5DBDB']
width = 0.2

m1_t[['device1','device2','comm']].plot(kind='bar', width = width, stacked=True, color=colors)
plt.ylabel("Count") #here
plt.xlabel('Device')

m1_t['data'].plot(secondary_y=True, color='darkslategrey', marker='o',MarkerSize= 3)
ax = plt.gca()
plt.xlim([-width, len(m1_t['comm'])-width])
ax.set_xticklabels(("1", "2", "3"))
plt.ylabel("data (MB)")

plt.show()

enter image description here

相关问题