文章目录
-
- 折线图plot
- 多个绘图区
- 绘制数学函数图像
- 散点图scatter
- 柱状图bar
- 直方图histogram
- 饼图pie
- 总结
折线图plot
import matplotlib.pyplot as plt
plt.figure(figsize=(15, 6), dpi=80)
plt.plot([1, 0, 9], [4, 5, 6])
plt.show()
![day01_matplotlib_demo_第1张图片](http://img.e-com-net.com/image/info8/16a05b454ae8484aadb4bcedc884e10b.jpg)
plt.figure(figsize=(15, 6), dpi=80)
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 17, 29, 25, 34, 23, 18])
plt.savefig('test78.jpg')
plt.show()
![day01_matplotlib_demo_第2张图片](http://img.e-com-net.com/image/info8/7ad8d56aa6654a0c8048360b2b95db1d.jpg)
import matplotlib.pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 5) for i in x]
plt.figure(figsize=(10, 4), dpi=80)
plt.plot(x, y_shanghai, 'b-.', label='上海')
plt.plot(x, y_beijing, 'r--', label='北京')
plt.legend()
plt.yticks(range(0, 40, 5))
x_label = ['11点{}分'.format(i) for i in x]
plt.xticks(x[::5], x_label[::5])
plt.xlabel('时间变化')
plt.ylabel('温度变化')
plt.title('某城市的11点钟的温度的变化')
plt.grid(True, 'both', 'x')
plt.show()
![day01_matplotlib_demo_第3张图片](http://img.e-com-net.com/image/info8/dc2b6dc4e0ff4f178e96e4f2e40ad40c.jpg)
多个绘图区
import matplotlib.pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]
figure, axes = plt.subplots(1, 2, figsize=(15, 6), dpi=80)
axes[0].plot(x, y_shanghai, 'r--', label='上海')
axes[1].plot(x, y_beijing, 'b-', label='北京')
axes[0].legend()
axes[1].legend()
axes[0].set_yticks(range(0, 40, 5))
axes[0].set_xticks(x[::5], ["11.{} ".format(i) for i in x][::5])
axes[1].set_yticks(range(0, 10, 2))
axes[1].set_xticks(x[::5], ["11.{} ".format(i) for i in x][::5])
axes[0].grid(visible=True, which='both', axis='x')
axes[1].grid(visible=True, which='both', axis='x')
axes[0].set_xlabel('时间')
axes[0].set_ylabel('温度')
axes[0].set_title('上海天气')
axes[1].set_xlabel('时间')
axes[1].set_ylabel('温度')
axes[1].set_title('北京天气')
plt.show()
![day01_matplotlib_demo_第4张图片](http://img.e-com-net.com/image/info8/76ebafb5056147a9b2eb237ca4765c51.jpg)
绘制数学函数图像
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 1000)
y = 2 * x * x
plt.figure(figsize=(15, 6), dpi=80)
plt.plot(x, y, 'r-', label='y=2x*x')
plt.legend(loc=1)
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.title('显示函数图像')
plt.grid(linestyle='--', alpha=0.5)
plt.show()
![day01_matplotlib_demo_第5张图片](http://img.e-com-net.com/image/info8/204b0d3168194f2a836918c9a9974260.jpg)
散点图scatter
import matplotlib.pyplot as plt
import numpy as np
x = [225.63, 247.07, 253.12, 457.85, 241.58, 303.23, 304.32, 340.34, 220.34, 240.23, 280.45]
y = [196.63, 203.88, 210.75, 374.74, 202.41, 299.35, 300.12, 330.34, 220.34, 222.45, 290.21]
plt.figure(figsize=(15, 6), dpi=80)
plt.scatter(x, y, alpha=0.5)
plt.show()
![day01_matplotlib_demo_第6张图片](http://img.e-com-net.com/image/info8/6dc224e6bbd84a588442117436c6f3a9.jpg)
柱状图bar
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
movie_names = ['雷神3', '正义联盟', '东方快车', '环游记', '全球风暴', '降魔传']
movie_data = [73853, 557767, 22354, 15969, 14839, 8725]
bar_labels = ['red', 'blue', '_red', 'orange', '_bue', '_orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange', 'tab:blue', 'tab:orange']
plt.figure(figsize=(15, 6), dpi=80)
bar1 = plt.bar(range(len(movie_names)), movie_data, label=bar_labels, color=bar_colors)
plt.bar_label(bar1, label_type='center')
plt.legend(title='电影')
plt.xticks(range(len(movie_names)), movie_names)
plt.xlabel('电影名称')
plt.ylabel('票房')
plt.title('部分电影票房柱状图')
plt.show()
![day01_matplotlib_demo_第7张图片](http://img.e-com-net.com/image/info8/a2d9e54414a64b28b3fa506ef5607881.jpg)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
movie_name = ['雷神3', '正义联盟', '东方快车']
first_day = [10587.6, 10062.5, 1275.7]
week_day = [36224.9, 34479.6, 11830]
figure, axes = plt.subplots(figsize=(15, 6), dpi=80)
p1 = axes.bar(range(len(movie_name)), first_day, label='first_day', width=0.2)
axes.bar_label(p1, label_type='center', color='b')
p2 = axes.bar([0.2, 1.2, 2.2], week_day, label='week_day', width=0.2)
axes.bar_label(p2, label_type='center', color='r')
axes.legend()
axes.set_xticks([0.1, 1.1, 2.1], movie_name);
plt.show()
![day01_matplotlib_demo_第8张图片](http://img.e-com-net.com/image/info8/64b14171d925459a873a00b4c4e46f18.jpg)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
movie_name = ['雷神3', '正义联盟', '东方快车']
first_day = [10587.6, 10062.5, 1275.7]
week_day = [36224.9, 34479.6, 11830]
figure, axes = plt.subplots(figsize=(15, 6), dpi=80)
p1 = axes.bar(range(len(movie_name)), first_day, label='first_day', width=0.2, bottom=[0, 0, 0], color=['r', 'g', 'b'])
axes.bar_label(p1, label_type='center', color='w')
p2 = axes.bar(range(len(movie_name)), week_day, label='week_day', width=0.2, bottom=first_day, color=['b', 'r', 'g'])
axes.bar_label(p2, label_type='center', color='w')
axes.legend()
axes.set_xticks(range(len(movie_name)), movie_name);
plt.show()
![day01_matplotlib_demo_第9张图片](http://img.e-com-net.com/image/info8/9aa06667277c41489a8e3c5190e704fa.jpg)
直方图histogram
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
movie_time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, \
101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86, \
95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137, \
123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115, \
132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102, \
123, 107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127, \
115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134, \
106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103, \
130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113, 134, \
106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, \
133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]
group_distance = 10
num_bins = (max(movie_time)-min(movie_time))//group_distance + 1
fig, axes = plt.subplots(figsize=(12, 8), dpi=80)
p = axes.hist(movie_time, num_bins, rwidth=1)
axes.set_xticks(range(min(movie_time), max(movie_time) + group_distance, group_distance))
axes.set_xlabel('影视播放时长分钟', size=16)
axes.set_ylabel('影视个数', size=16)
axes.set_title('影视时间分布直方图', size=18)
plt.grid()
plt.show()
![day01_matplotlib_demo_第10张图片](http://img.e-com-net.com/image/info8/091e9ce3342e4937bf835fd2b2323bab.jpg)
饼图pie
import matplotlib.pyplot as plt
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]
fig, axes = plt.subplots(figsize=(15, 8), dpi=80)
axes.pie(place_count, labels=movie_name, autopct='%1.2f%%', colors=['b', 'r','g','y','c','m','y','k','c','g','g'])
axes.axis('equal')
axes.legend(loc='best', title="Ingredients")
plt.title("排片占比示意图", size=18)
plt.show()
![day01_matplotlib_demo_第11张图片](http://img.e-com-net.com/image/info8/950376a2f3134dd2b85c675e2f02a7a9.jpg)
总结
- 容器层
- 画板层Canvas
- 画布层plt.figure(figsize=(), dpi=)
- 绘画层/坐标轴fig, axes = plt.subplots(nrows=, ncols=, figsize=(). dpi=)
- 辅助显示层
- 修改x,y轴刻度 plt.xticks()
- 添加描述信息 plt.xlabel() plt.ylabel() plt.title()
- 添加网格 plt.grid()
- 显示图例 plt.legend()
- 图像层,可以设置图像颜色、风格、标签
- 折线图 plt.plot()
- 散点图 plt.scatter()
- 柱状图 plt.bar()
- 直方图 plt.hist()
- 饼图 plr.pie()