鱼C论坛

 找回密码
 立即注册
查看: 1294|回复: 2

[已解决]用matplotlib绘制的条形图,条形重合

[复制链接]
发表于 2021-12-10 15:36:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pandas as pd
  2. import matplotlib.pyplot as plt


  3. phones = pd.read_excel('D:\测试.xlsx',)


  4. plt.rcParams ['font.sans-serif'] = ['SimHei']
  5. plt.rcParams ['axes.unicode_minus'] = False


  6. x = phones['品牌']
  7. y = phones['淘宝销量']
  8. z = phones['京东销量']


  9. plt.bar(x,y,color='g',label="淘宝销量",alpha=0.5,width=0.4)
  10. plt.bar(x,z,color='b',label='京东销量',width=0.4)

  11. plt.legend()
  12. plt.show()
复制代码




excel表就是以下这些内容
品牌        淘宝销量        京东销量
小米            9                   8
荣耀           20                   30
华为            5                   4


代码运行后出现的图表两条柱状是重合的,
直接在x后加数字会报错TypeError: can only concatenate str (not "float") to str
请问应该用什么方法才能将两个条形分开,
求解答


最佳答案
2021-12-10 16:07:11
直接抄官网
  1. import matplotlib.pyplot as plt

  2. plt.rcParams['font.sans-serif'] = ['SimHei']
  3. plt.rcParams['axes.unicode_minus'] = False

  4. labels = ['小米', '荣耀', '华为']
  5. taobao = [9, 20, 5]
  6. jingdong = [8, 30, 4]
  7. x = [1, 2, 3]
  8. width = 0.35  # the width of the bars

  9. fig, ax = plt.subplots()
  10. rects1 = ax.bar([i - width / 2 for i in x], taobao, width, label='淘宝')
  11. rects2 = ax.bar([i + width / 2 for i in x], jingdong, width, label='京东')

  12. # Add some text for labels, title and custom x-axis tick labels, etc.
  13. ax.set_ylabel('销量')
  14. ax.set_title('销量对比')
  15. ax.set_xticks(x)
  16. ax.set_xticklabels(labels)
  17. ax.legend()


  18. def autolabel(rects):
  19.     """Attach a text label above each bar in *rects*, displaying its height."""
  20.     for rect in rects:
  21.         height = rect.get_height()
  22.         ax.annotate('{}'.format(height),
  23.                     xy=(rect.get_x() + rect.get_width() / 2, height),
  24.                     xytext=(0, 3),  # 3 points vertical offset
  25.                     textcoords="offset points",
  26.                     ha='center', va='bottom')


  27. autolabel(rects1)
  28. autolabel(rects2)

  29. fig.tight_layout()

  30. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-10 16:07:11 | 显示全部楼层    本楼为最佳答案   
直接抄官网
  1. import matplotlib.pyplot as plt

  2. plt.rcParams['font.sans-serif'] = ['SimHei']
  3. plt.rcParams['axes.unicode_minus'] = False

  4. labels = ['小米', '荣耀', '华为']
  5. taobao = [9, 20, 5]
  6. jingdong = [8, 30, 4]
  7. x = [1, 2, 3]
  8. width = 0.35  # the width of the bars

  9. fig, ax = plt.subplots()
  10. rects1 = ax.bar([i - width / 2 for i in x], taobao, width, label='淘宝')
  11. rects2 = ax.bar([i + width / 2 for i in x], jingdong, width, label='京东')

  12. # Add some text for labels, title and custom x-axis tick labels, etc.
  13. ax.set_ylabel('销量')
  14. ax.set_title('销量对比')
  15. ax.set_xticks(x)
  16. ax.set_xticklabels(labels)
  17. ax.legend()


  18. def autolabel(rects):
  19.     """Attach a text label above each bar in *rects*, displaying its height."""
  20.     for rect in rects:
  21.         height = rect.get_height()
  22.         ax.annotate('{}'.format(height),
  23.                     xy=(rect.get_x() + rect.get_width() / 2, height),
  24.                     xytext=(0, 3),  # 3 points vertical offset
  25.                     textcoords="offset points",
  26.                     ha='center', va='bottom')


  27. autolabel(rects1)
  28. autolabel(rects2)

  29. fig.tight_layout()

  30. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-12-10 17:07:52 | 显示全部楼层

感谢回答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 23:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表