使用matplotlib绘制折线图,柱状图,柱线混合图「建议收藏」

您所在的位置:网站首页 柱状图和折线图怎么做在一起 使用matplotlib绘制折线图,柱状图,柱线混合图「建议收藏」

使用matplotlib绘制折线图,柱状图,柱线混合图「建议收藏」

2024-07-09 22:57| 来源: 网络整理| 查看: 265

大家好,又见面了,我是你们的朋友全栈君。

文章目录 matplotlib介绍matplotlib绘制折线图matplotlib绘制柱状图matplotlib绘制柱线混合图matplotlib介绍Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用,如 PyQt 和 wxPython。安装Matplotlib库命令:在cmd命令窗口输入pip install matplotlib。matplotlib绘制折线图绘制一条折线的折线图代码语言:javascript复制# -*- coding:utf-8 -*- import matplotlib import matplotlib.pyplot as plt # 处理乱码 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文 x = [1, 2, 3, 4] y = [10, 50, 20, 100] # "r" 表示红色,ms用来设置*的大小 plt.plot(x, y, "r", marker='*', ms=10, label="a") # plt.plot([1, 2, 3, 4], [20, 30, 80, 40], label="b") plt.xticks(rotation=45) plt.xlabel("发布日期") plt.ylabel("小说数量") plt.title("80小说网活跃度") # upper left 将图例a显示到左上角 plt.legend(loc="upper left") # 在折线图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式 for x1, y1 in zip(x, y): plt.text(x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=20, rotation=0) plt.savefig("a.jpg") plt.show()

图形效果展示:

在这里插入图片描述在这里插入图片描述注意:savefig()是图形存储成图片,show()是将图形显示出来。绘制多条折线代码语言:javascript复制# -*- coding:utf-8 -*- import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文 x = [1, 2, 3, 4] y1 = [45, 50, 20, 100] y2 = [26, 10, 76, 25] y3 = [11, 66, 55, 88] y4 = [69, 50, 35, 100] plt.plot(x, y1, marker='*', ms=10, label="a") plt.plot(x, y2, marker='*', ms=10, label="b") plt.plot(x, y3, marker='*', ms=10, label="c") plt.plot(x, y4, marker='*', ms=10, label="d") plt.xticks(rotation=45) plt.xlabel("发布日期") plt.ylabel("小说数量") plt.title("80小说网活跃度") plt.legend(loc="upper left") # 在折线图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式 for y in [y1, y2, y3, y4]: for x1, yy in zip(x, y): plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) plt.savefig("a.jpg") plt.show()

图形效果展示:

在这里插入图片描述在这里插入图片描述matplotlib绘制柱状图绘制普通柱状图代码语言:javascript复制# -*- coding:utf-8 -*- import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文 # 构建数据 x = [1, 2, 3, 4] y = [450, 500, 200, 1000] # 绘图 plt.bar(x=x, height=y, label='书库大全', color='steelblue', alpha=0.8) # 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式 for x1, yy in zip(x, y): plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) # 设置标题 plt.title("80小说网活跃度") # 为两条坐标轴设置名称 plt.xlabel("发布日期") plt.ylabel("小说数量") # 显示图例 plt.legend() plt.savefig("a.jpg") plt.show()

图形效果展示:

在这里插入图片描述在这里插入图片描述绘制多组柱状图代码语言:javascript复制# -*- coding:utf-8 -*- import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文 # 构建数据 x = ['2015', '2016', '2017', '2018', '2019'] y1 = [4500, 5000, 2000, 7000, 10000] y2 = [5200, 7000, 5000, 9000, 11000] # 绘图 plt.bar(x=x, height=y1, label='python', color='steelblue', alpha=0.8) plt.bar(x=x, height=y2, label='java', color='indianred', alpha=0.8) # 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式 for x1, yy in zip(x, y1): plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) for x1, yy in zip(x, y2): plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) # 设置标题 plt.title("python与java图书对比") # 为两条坐标轴设置名称 plt.xlabel("年份") plt.ylabel("销量") # 显示图例 plt.legend() plt.savefig("a.jpg") plt.show()

图形效果展示:

在这里插入图片描述在这里插入图片描述绘制柱状图的条柱并列显示代码语言:javascript复制# -*- coding:utf-8 -*- import numpy as np import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文 # 构建数据 x = ['2015', '2016', '2017', '2018', '2019'] y1 = [4500, 5000, 2000, 7000, 10000] y2 = [5200, 7000, 5000, 9000, 11000] bar_width = 0.3 # 将X轴数据改为使用range(len(x_data), 就是0、1、2... plt.bar(x=range(len(x)), height=y1, label='python', color='steelblue', alpha=0.8, width=bar_width) # 将X轴数据改为使用np.arange(len(x_data))+bar_width, # 就是bar_width、1+bar_width、2+bar_width...这样就和第一个柱状图并列了 plt.bar(x=np.arange(len(x)) + bar_width, height=y2, label='java', color='indianred', alpha=0.8, width=bar_width) # 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式 for x1, yy in enumerate(y1): plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) for x1, yy in enumerate(y2): plt.text(x1 + bar_width, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) # 设置标题 plt.title("python与java对比") # 为两条坐标轴设置名称 plt.xlabel("年份") plt.ylabel("销量") # 显示图例 plt.legend() plt.savefig("a.jpg") plt.show()

图形效果展示:

在这里插入图片描述在这里插入图片描述matplotlib绘制柱线混合图绘制柱线混合图代码语言:javascript复制# -*- coding:utf-8 -*- import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文 # 构建数据 x = [2, 4, 6, 8] y = [450, 500, 200, 1000] # 绘图 plt.bar(x=x, height=y, label='书库大全', color='steelblue', alpha=0.8) # 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式 for x1, yy in zip(x, y): plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0) # 设置标题 plt.title("80小说网活跃度") # 为两条坐标轴设置名称 plt.xlabel("发布日期") plt.ylabel("小说数量") # 显示图例 plt.legend() # 画折线图 plt.plot(x, y, "r", marker='*', ms=10, label="a") plt.xticks(rotation=45) plt.legend(loc="upper left") plt.savefig("a.jpg") plt.show()

图形效果展示:

在这里插入图片描述在这里插入图片描述

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/138667.html原文链接:https://javaforall.cn



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3