Matplotlib基本图表绘制
折线图
from matplotlib import pyplot as plt
import matplotlib
font = {'family': 'MicroSoft YaHei',
'weight': 'bold',
'size': 10}
matplotlib.rc('lines', lw=2)
matplotlib.rc('font', **font)
fig = plt.figure(figsize=(20, 8), dpi=80)
x = range(2, 26, 2)
y_1 = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]
y_2 = [0, 3, 7, 4, -1, 5, 8, 2, 3, 2, 5, 0]
plt.plot(x, y_1, label="苏州", color="pink", linestyle="-.")
plt.plot(x, y_2, label="哈尔滨", color='c', linestyle=':')
_xtick_labels = ["10月{}日".format(i) for i in range(2, 26, 2)]
plt.xticks(x, _xtick_labels, rotation=45)
plt.yticks(range(min(y_2), max(y_1)+1))
plt.grid(alpha=1, linestyle="--")
plt.legend(loc="upper left")
plt.xlabel("日期")
plt.ylabel("温度/℃")
plt.title("10月气温变化情况")
plt.savefig("./sig_size.png")
plt.show()

更多外观
外观类型
关键字参数 |
含义 |
lw(lineweight) |
线条宽度 |
ls(linestyle) |
线条样式 |
c(color) |
线条颜色 |
fc(facecolor) |
图表填充颜色 |
ec(edgecolor) |
图表边缘颜色 |
mew(markeredgewidth) |
标记边缘宽度 |
aa(antialiased) |
抗锯齿 |
颜色
类别 |
举例 |
字符 |
r(红色) |
字符串 |
black(黑色) |
16进制 |
#000000 (白色) |
线条样式
字符 |
类型 |
- |
实线 |
– |
破折线 |
-. |
点划线 |
: |
点虚线 |
‘’ |
无线条 |
图例位置
图例中loc关键字参数的值可以是字符串、数字或坐标元组。
字符串 |
数字 |
best |
0 |
upper right |
1 |
upper left |
2 |
lower left |
3 |
lower right |
4 |
right |
5 |
center left |
6 |
center right |
7 |
lower center |
8 |
upper center |
9 |
center |
10 |
使用本地字体
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
plt.xticks(x, _xtick_labels, rotation=45, fontproperties=my_font)
散点图
plt.scatter(x, y_1, label="苏州", color="pink")
plt.scatter(x, y_2, label="哈尔滨", color='c')

条形图
from matplotlib import pyplot as plt
font = {'family': 'MicroSoft YaHei',
'weight': 'bold',
'size': 15}
matplotlib.rc("font", **font)
fig = plt.figure(figsize=(20, 8), dpi=80)
a = ["黑神话:悟空", "最终幻想7:重生", "艾尔登法环:黄金树幽影", "小丑牌", "暗喻幻想", "宇宙机器人"]
b_1 = [81, 92, 94, 90, 94, 94]
b_2 = [83, 90, 81, 88, 88, 92]
bar_width = 0.3
x = list(range(len(a)))
x_1 = [i-bar_width*0.5 for i in x]
x_2 = [i+bar_width*0.5 for i in x]
plt.bar(x_1, b_1, width=0.3, color="orange", label="媒体")
plt.bar(x_2, b_2, width=0.3, color="red", label="用户")
plt.xticks(range(len(a)), a)
plt.grid(alpha=0.3)
plt.legend(loc="upper left")
plt.title("TGA年度游戏提名M站评分")
plt.show()

横向条形图
在竖向条形图代码的基础上作如下修改:
plt.barh(x_1, b_1, height=0.3, color="orange", label="媒体")
plt.barh(x_2, b_2, height=0.3, color="red", label="用户")
plt.yticks(range(len(a)), a)
plt.legend(loc="lower right")

直方图
from matplotlib import pyplot as plt
import matplotlib
font = {'family': 'MicroSoft YaHei',
'weight': 'bold',
'size': 15}
matplotlib.rc("font", **font)
fig = plt.figure(figsize=(15, 8), dpi=80)
a = [15, 20, 15, 20, 25, 25, 30, 15, 30, 25,
15, 30, 25, 35, 30, 35, 30, 25, 20, 30,
20, 25, 35, 30, 25, 20, 30, 25, 35, 25,
15, 25, 35, 25, 25, 30, 35, 25, 35, 20,
30, 30, 15, 30, 40, 30, 40, 15, 25, 40,
20, 25, 20, 15, 20, 25, 25, 40, 25, 25,
40, 35, 25, 30, 20, 35, 20, 15, 35, 25,
25, 30, 25, 30, 25, 30, 43, 25, 43, 22,
20, 23, 20, 25, 15, 25, 20, 25, 30, 43,
35, 45, 30, 45, 30, 45, 45, 35]
d = 5
num_bins = (max(a)-min(a))//d
plt.hist(a, num_bins)
plt.xticks(range(min(a), max(a)+d, d))
plt.grid(alpha=0.3)
plt.show()

频率分布直方图
plt.hist(a, num_bins, density=True)
