首页 > 其他分享 >matplotlib条形图

matplotlib条形图

时间:2024-03-03 22:25:17浏览次数:20  
标签:plt bar 14 16 matplotlib width 15 条形图

matplotlib条形图

假设你获取到了2017年内地电影票房前20的电影(列表a)和电影票房数据(列表b), 那么如何更加直观的展示该数据?

from matplotlib import pyplot as plt

a = ["Wolf Warrior 2", "Fast and Furious 8", "Kung Fu Yoga", "Journey to the West", "Transformers 5:\n The Last Knight", "Dangal", "Pirates of the Caribbean 5:\n Dead Men Tell No Tales", "King Kong: Skull Island", "Xx: The Final Return", "Resident Evil 6:\n The Final Chapter", "Ride the Waves", "Despicable Me 3", "Taking Tiger Mountain Out of Mind", "Havoc in Tianzhu","Wolverine: The Last Stand", "Spider-Man: Homecoming", "Wukong", "Guardians of the Galaxy Vol. 2", "The Mummy", "The Mummy"]
b = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘制条形图
plt.bar(range(len(a)), b, width=0.2, color="orange")
# 设置x轴刻度
plt.xticks(range(len(a)), a, rotation=90)
plt.show()

output_2_0.png

# 绘制横着的条形图, 能更清楚的表示
a = ["Wolf Warrior 2", "Fast and Furious 8", "Kung Fu Yoga", "Journey to the West", "Transformers 5: The Last Knight", "Dangal", "Pirates of the Caribbean 5: Dead Men Tell No Tales", "King Kong: Skull Island", "Xx: The Final Return", "Resident Evil 6: The Final Chapter", "Ride the Waves", "Despicable Me 3", "Taking Tiger Mountain Out of Mind", "Havoc in Tianzhu","Wolverine: The Last Stand", "Spider-Man: Homecoming", "Wukong", "Guardians of the Galaxy Vol. 2", "The Mummy", "The Mummy"]
b = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘制横着的条形图, 参数由宽度变为高度
plt.barh(range(len(a)), b, height=0.2, color="orange")
# 设置y轴刻度, 且不需要旋转
plt.yticks(range(len(a)), a)
# 设置网格
plt.grid(alpha=0.3)
plt.show()

output_3_0.png

假设你知道了列表a中电影分别在2017-09-14(b_14), 2017-09-15(b_15), 2017-09-16(b_16)三天的票房, 为了展示列表中电影本身的票房以及同其他电影的数据对比情况, 应该如何更加直观的呈现该数据?

x = ["War for the Planet of the Apes", "Dunkirk", "Spider-Man: Homecoming", "Wolf Warrior 2"]
# b_i表示这四部电影在第i天的票房
b_14 = [2358,399,2358,362]
b_15 = [12357,156,2045,168]
b_16 = [15746,312,4497,319]

# 设置条形的宽度
bar_width = 0.2

x_14 = list(range(len(x)))
x_15 = [i+bar_width for i in x_14]
x_16 = [i+bar_width*2 for i in x_14]
print("x_14:", x_14)
print("x_15:", x_15)
print("x_16:", x_16)

plt.figure(figsize=(20, 8), dpi=80)
plt.bar(x_14, b_14, width=bar_width, label="September 14th")
plt.bar(x_15, b_15, width=bar_width, label="September 15th")
plt.bar(x_16, b_16, width=bar_width, label="September 16th")

# 添加x轴刻度
plt.xticks(x_15, x)
# 添加图例
plt.legend(loc="best")
plt.show()
x_14: [0, 1, 2, 3]
x_15: [0.2, 1.2, 2.2, 3.2]
x_16: [0.4, 1.4, 2.4, 3.4]

output_5_1.png

标签:plt,bar,14,16,matplotlib,width,15,条形图
From: https://www.cnblogs.com/xushengxiang/p/18050866

相关文章

  • matplotlib散点图
    matplotlib散点图假设通过爬虫你获取到了北京2016年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间(天)变化的某种规律?frommatplotlibimportpyplotaspltx_3=range(1,32)x_10=range(51,82)y_3=[11,17,16,11,12,11,12,6,6,7,......
  • Matplotlib调整图例中图形的大小
    一、问题描述在使用matplotlib画图的时候需要对图例中的图形进行放大缩写的操作,避免图例太小。问题如下:二、解决方法由于不同的对象解决方法有差异,这里对散点图和柱形图进行讨论。2.1散点图散点类型的图像可以使用下面的代码进行图形大小的调整。legend=plt.legend()#......
  • 【机器学习科学库】全md文档笔记:Matplotlib详细使用方法(已分享,附代码)
    本系列文章md笔记(已分享)主要讨论人工智能相关知识。主要内容包括,了解机器学习定义以及应用场景,掌握机器学习基础环境的安装和使用,掌握利用常用的科学计算库对数据进行展示、分析,学会使用jupyternotebook平台完成代码编写运行,应用Matplotlib的基本功能实现图形显示,应用Matplotlib......
  • Python嵌套绘图并为条形图添加自定义标注
    论文绘图时经常需要多图嵌套,正好最近绘图用到了,记录一下使用Python实现多图嵌套的过程。首先,实现Seaborn分别绘制折线图和柱状图。'''绘制折线图'''importseabornassnsimportmatplotlib.pyplotaspltimportwarningswarnings.filterwarnings("ignore","use_inf_as_n......
  • 【机器学习科学库】全md文档笔记:Jupyter Notebook和Matplotlib使用(已分享,附代码)
    本系列文章md笔记(已分享)主要讨论人工智能相关知识。主要内容包括,了解机器学习定义以及应用场景,掌握机器学习基础环境的安装和使用,掌握利用常用的科学计算库对数据进行展示、分析,学会使用jupyternotebook平台完成代码编写运行,应用Matplotlib的基本功能实现图形显示,应用Matplotlib......
  • python · matplotlib | seaborn 画图与调整图例位置
    1seaborn画图代码存档:sns.set_style("whitegrid")#好看的styleplt.figure()#plt.plot(ppo_data['Step']*step_mul,ppo_data['ppo_mean'],label='PPO')#plt.plot(sac_data['Step']*step_mul,sac_data['sac_m......
  • 匀加速运动模拟python,(matplotlib)
    importnumpyasnpimportmatplotlibmatplotlib.use("TKAgg")importmatplotlib.pyplotaspltg=9.8s=100ds=0.00001#单位米v0=0.001#m/sv=[v0]t=[ds/v0]t_sum=0ds_num=int(s/ds)x=[]y=[]foriinrange(ds_num+1):ifi==0:continue......
  • Visual Studio部署matplotlib绘图库的C++版本
      本文介绍在VisualStudio软件中配置、编译C++环境下matplotlibcpp库的详细方法。  matplotlibcpp库是一个C++环境下的绘图工具,其通过调用Python接口,实现在C++代码中通过matplotlib库的命令绘制各类图像。由于其需要调用Python接口,因此在配置matplotlibcpp库时有些较为麻烦......
  • matplotlib annotate
    matplotlibannotate参考:matplotlibannotate在数据可视化中,常常需要在图表中添加标注,以便更清楚地表达数据的含义。Matplotlib库中的annotate()函数提供了一种简单的方法来添加标注。1.annotate()函数的基本语法annotate()函数的基本语法如下:annotate(text,xy,xytext,a......
  • 轴调控大揭秘:Matplotlib轴设置全攻略+顺口溜,一文掌握!
    在数据可视化的世界里,Matplotlib是那把魔法棒,让枯燥的数据跃然纸上,而掌控这把魔法棒的核心,就是对坐标轴的精妙操作。今天,就让我们一起揭开Matplotlib坐标轴设置的神秘面纱,配上易记的顺口溜,让你的数据可视化之路畅通无阻!一、轴标签和标题:基础篇xlabel&ylabel:设定X轴和Y轴的标......