赞
踩
本文分享自华为云社区《画图实战-Python实现某产品全年销量数据多种样式可视化》,作者:虫无涯。
A、X轴和Y轴;
B、X轴和Y轴刻度;
C、X轴和Y轴标签;
D、绘图区域。
A、hold属性默认为True,可在一幅图中绘制多个曲线;
B、将hold属性修改为False,每一个plot都会覆盖前面的plot(这种方法不推荐,建议使用默认的)。
A、可使用grid方法为图添加网格线;
B、还可以使用其他方法,如axis方法、xlim方法、ylim方法、legend方法;
matplotlib配置信息是从配置文件读取的。在配置文件中可以为matplotlib的几乎所有属性指定永久有效的默认;
主要为永久配置和动态配置。
直接使用pip安装即可:
pip install matplotlib
某产品 | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
---|---|---|---|---|---|---|---|---|---|---|---|---|
订单量(indent) | 15 | 33 | 42 | 50 | 115 | 220 | 86 | 66 | 59 | 43 | 39 | 50 |
退货量(returned) | 6 | 13 | 18 | 23 | 55 | 98 | 42 | 31 | 25 | 22 | 17 | 24 |
- month = ["Jan", "Feb", "Mar", "Apr",
- "May", "Jun", "Jul", "Aug",
- "Sep", "Oct", "Nov", "Dec"]
-
- print(f"月份为:{month}")
- # 输出:月份为:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
复制运行
- # 订单量
- indent = [15, 33, 42, 50, 115, 20, 86, 66, 59, 43, 39, 50]
-
- # 退货量
- returned = [6, 13, 18, 23, 55, 98, 42, 31, 25, 22, 17, 24]
-
- print(f"每月订单量为:{indent}")
- print(f"每月退货量为:{returned}")
x1 = month y1 = indent
x2= month y2 = returned
- import subprocess
- import sys
- subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
- import matplotlib.pyplot as plt
-
- month = ["Jan", "Feb", "Mar", "Apr",
- "May", "Jun", "Jul", "Aug",
- "Sep", "Oct", "Nov", "Dec"]
- print(f"月份为:{month}")
-
- # 订单量
- indent = [15, 33, 42, 50, 115, 20, 86, 66, 59, 43, 39, 50]
-
- # 退货量
- returned = [6, 13, 18, 23, 55, 98, 42, 31, 25, 22, 17, 24]
- print(f"每月订单量为:{indent}")
- print(f"每月退货量为:{returned}")
-
- # 绘制折线图
- plt.plot(month, indent, label='订单量',
- linewidth=2, color='r', marker='o',
- markerfacecolor='blue', markersize=8)
-
- plt.plot(month, returned, label='退货量',
- linewidth=2, color='y', marker='o',
- markerfacecolor='blue', markersize=8)
-
- plt.xlabel('月份')
- plt.ylabel('数量')
- plt.title('某产品全年订单销售情况')
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.legend()
- # plt.show()
- plt.savefig("plot.jpg")
订单量(indent/m1) | 10/5 | 30/7 | 50/9 | 70/11 | 90/13 | 110/15 | 130/17 | 150/19 |
---|---|---|---|---|---|---|---|---|
退货量(returned/m2) | 20/3 | 40/5 | 60/7 | 80/9 | 10011 | 120/13 | 140/15 | 160/17 |
x1 = indent y1 = m1
x2= returned y2 = m2
- import subprocess
- import sys
- subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
- import matplotlib.pyplot as plt
-
- # 订单量
- indent = [10, 30, 50, 70, 90, 110, 130, 150]
- # 销售额
- m1 = [5, 7, 9, 11, 13, 15, 17, 19]
-
- # 退货量
- returned = [20, 40, 60, 80, 100, 120, 140, 160]
- # 价格
- m2 = [3, 5, 7, 9, 11, 13, 15, 17]
-
- # 绘制折线图
- plt.bar(indent, m1, width=3, label='订单量-销售额', color='r', )
- plt.bar(returned, m2, width=3, label='退货量-价格', color='y')
-
- plt.xlabel('数量')
- plt.ylabel('价格')
- plt.title('某产品全年订单&销售额情况')
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.legend()
- # plt.show()
- plt.savefig("plot.jpg")
注意:后续的数据和操作逻辑和前边的一样,为了快速了解其使用,不再描述详细的数据,仅用示例说明。
- import subprocess
- import sys
- subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
- import matplotlib.pyplot as plt
-
- data = [15, 33, 42, 50, 115, 20, 86, 66, 59, 43, 39, 50]
- x = range(0, 100, 2)
-
- # 绘制直方图
- plt.hist(data, x, rwidth=3, label='直方图', color='y')
-
- plt.xlabel('X')
- plt.ylabel('Y')
- plt.title('直方图')
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.legend()
- # plt.show()
- plt.savefig("plot.jpg")
- import subprocess
- import sys
- subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
- import matplotlib.pyplot as plt
-
- data = [15, 33, 42, 50, 115, 20, 86, 66, 59, 43, 39, 50]
- x = range(0, len(data))
-
- # 绘制散点图
- plt.scatter(x, data, label='散点图', s=15)
-
- plt.xlabel('X')
- plt.ylabel('Y')
- plt.title('散点图')
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.legend()
- # plt.show()
- plt.savefig("plot.jpg")
- import subprocess
- import sys
- subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
- subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
- import matplotlib.pyplot as plt
- import numpy as np
-
- data = np.array([10, 20, 15, 15, 5, 5, 30])
-
- plt.pie(data,
- labels=['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'],
- colors=["#8B008B", "#FF1493", "#4B0082", "#B0C4DE", "#E1FFFF", "#008080", "#00FF7F"],
- explode=(0, 0, 0.3, 0, 0, 0.2, 0),
- autopct='%.2f%%',
- )
- plt.title('饼图')
- plt.rcParams['font.sans-serif'] = ['SimHei']
- # plt.show()
- plt.savefig("plot.jpg")
- import subprocess
- import sys
- subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
- subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
- import matplotlib.pyplot as plt
- import numpy as np
-
- x = np.array([10, 50])
- y = np.array([10, 80])
- plt.subplot(2, 2, 1)
- plt.plot(x, y)
- plt.title("图1")
-
- x = np.array([10, 20, 30, 40])
- y = np.array([10, 30, 50, 110])
- plt.subplot(2, 2, 2)
- plt.plot(x, y)
- plt.title("图2")
- x = np.array([10, 20, 30, 40])
- y = np.array([50, 60, 70, 80])
- plt.subplot(2, 2, 3)
- plt.plot(x, y)
- plt.title("图3")
- x = np.array([20, 25, 30, 35])
- y = np.array([40, 45, 50, 55])
- plt.subplot(2, 2, 4)
- plt.plot(x, y)
- plt.title("图4")
-
- plt.suptitle("多图显示")
- plt.rcParams['font.sans-serif'] = ['SimHei']
- #plt.show()
- plt.savefig("plot.jpg")
Python实现某产品全年销量数据多种样式可视化,主要是应用了python的matplotlib库进行绘制各种图表,除了以上的几种图表,还有柱状图、网格图等等。学习的时候建议使用真实的数据,可以真正达到分析问题的效果。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。