figure :n. 数字,数目,数值;数字符号,位数;身材,体型;(远处的)人影;(重要的)人物;(绘画或模型中)人像;(书中的)图、表;(数学)图形;价格,金额
plot :n. 阴谋,密谋;情节;(专用的)小块地;(表现两个变量关系的)图表;<美>图表,地图;<美>底层平面图
所以,fig=plt.figure(num=1,figsize=(6,4)) 是创建一个图形底板;
ax=fig.add_subplot() 是在图形上创建一个区域;
可以在图形上直接画图
import matplotlib.pyplot as plt
plt.figure(num=1,figsize=(4,4))
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
plt.plot(x, y, format_string, **kwargs)
参数 | 说明 |
---|---|
x | X轴数据,列表或数组,可选 |
y | Y轴数据,列表或数组 |
format_string | 控制曲线的格式字符串,可选 |
**kwargs | 第二组或更多(x,y,format_string),可画多条曲线 |
format_string
由颜色字符、风格字符、标记字符组成
- 颜色字符
'b'
蓝色'm'
洋红色 magenta'g'
绿色'y'
黄色'r'
红色'k'
黑色'w'
白色'c'
青绿色 cyan'#008000'
RGB某颜色'0.8'
灰度值字符串- 多条曲线不指定颜色时,会自动选择不同颜色
- 风格字符
'‐'
实线'‐‐'
破折线'‐.'
点划线':'
虚线'' ' '
无线条
- 标记字符
'.'
点标记','
像素标记(极小点)'o'
实心圈标记'v'
倒三角标记'^'
上三角标记'>'
右三角标记'<'
左三角标记...等等
也可以在图形的小区域上画图
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(4, 4))
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
ax = fig.add_subplot()
ax.plot(x, y)
plt.show()
想要划分多个小区域,可以使用 ax = fig.add_subplot(325) # 劃分3行2列區域,取第5個
add_subplot(xyz)第一个数字x代表行数,第二个数字y代表列数,第三个数字z表示取第z个小区域(先取第一行,从左到右)。
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(9, 9))
ax1 = fig.add_subplot(221) # 劃分2行2列區域,取第1個
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax2 = fig.add_subplot(233) # 劃分2行3列區域,取第3個
ax2.plot([1, 2, 3, 4], [2, 2, 3, 4])
ax3 = fig.add_subplot(325) # 劃分3行2列區域,取第5個
ax3.plot([1, 2, 3, 4], [1, 2, 2, 4])
ax4 = fig.add_subplot(224) # 劃分2行2列區域,取第4個
ax4.plot([1, 2, 3, 4], [1, 2, 3, 3])
plt.show()
很显然,上边那种方式划分11行9列就做不到。
将参数间省略的逗号加上去就可以了。
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(9, 9))
ax1 = fig.add_subplot(11, 9, 1) # 劃分11行9列區域,取第1個
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax2 = fig.add_subplot(2, 3, 3) # 劃分2行3列區域,取第3個
ax2.plot([1, 2, 3, 4], [2, 2, 3, 4])
ax3 = fig.add_subplot(3, 2, 5) # 劃分3行2列區域,取第5個
ax3.plot([1, 2, 3, 4], [1, 2, 2, 4])
ax4 = fig.add_subplot(2, 2, 4) # 劃分2行2列區域,取第4個
ax4.plot([1, 2, 3, 4], [1, 2, 3, 3])
plt.show()
另外一种划分区域的方法,相当于先做一个网格,然后进行跨行跨列合并。
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure(num=1, figsize=(6, 6)) # 创建画布
gs = gridspec.GridSpec(3, 3) # 设定3*3的网格,使用下标时从0开始
ax1 = fig.add_subplot(gs[0, :]) # 第1行,所有列
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax2 = fig.add_subplot(gs[1, :-1]) # 第2行,倒数第一列之前的所有列
ax2.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax3 = fig.add_subplot(gs[1:, -1]) # 第2行之后的所有行,倒数第一列
ax3.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax4 = fig.add_subplot(gs[2, 0]) # 第3行,第1列
ax4.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax5 = fig.add_subplot(gs[2, 1]) # 第3行,第2列
ax5.plot([1, 2, 3, 4], [1, 2, 3, 4])
plt.show()
注意, 子区域可能会相互重合,并且后创建的子区域会覆盖在先创建的子区域上面。
下边我们手动限制刻度范围,set_xlim()、set_ylim()
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(6, 6))
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_title('exp') # 添加标题
ax1.set_xlim(1, 7.1) # x轴限制坐标从1到7.1
ax1.set_ylim(-10, 10) # y轴限制坐标从-10到10
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
plt.show()
上边我们都是自动生成的刻度,现在我们手动设置刻度,set_xticks()、set_yticks()
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(6, 6))
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_title('exp') # 添加标题
ax1.set_xticks([1, 1.5, 3, 4, 5, 6, 8, 10]) # x轴刻度
ax1.set_yticks([5, 10, 20, 30, 40]) # y轴刻度
ax1.set_xlim(1, 7.1) # x轴限制坐标从1到7.1
ax1.set_ylim(-10, 10) # y轴限制坐标从-10到10
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
plt.show()
后设置的坐标范围,会限制刻度的显示,如果先设置坐标范围,再设置刻度,情况会不一样
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(6, 6))
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_title('exp') # 添加标题
ax1.set_xlim(1, 7.1) # x轴限制坐标从1到7.1
ax1.set_ylim(-10, 10) # y轴限制坐标从0到100
ax1.set_xticks([1, 1.5, 3, 4, 5, 6, 8, 10]) # x轴刻度
ax1.set_yticks([5, 10, 20, 30, 40]) # y轴刻度
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
plt.show()
set_xticklabels()可以将X轴对应数值变成label,对应Y轴方法为set_yticklabels()
注意,如果不设置set_xticks()只设置set_xticklabels(),则X轴会自动平均设置刻度,并将相应的label附上
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(6, 6))
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_title('exp') # 添加标题
ax1.set_xlim(1, 7.1) # x轴限制坐标从1到7.1
ax1.set_ylim(-10, 10) # y轴限制坐标从0到100
ax1.set_xticks([1, 1.5, 3, 4, 5, 6, 8, 10]) # x轴刻度
ax1.set_yticks([5, 10, 20, 30, 40]) # y轴刻度
ax1.set_xticklabels(['one', 'two', 'four', 'six', 'eight', 'ten', 'x', 'xx'])
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
plt.show()
使用 ax2 = ax1.twinx() 创建右侧Y轴对应的图形,同样,有 ax1.twiny()方法
import matplotlib.pyplot as plt
fig = plt.figure(num=1, figsize=(6, 6))
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_title('exp') # 添加标题
ax1.set_xlim(1, 7.1) # x轴限制坐标从1到7.1
ax1.set_ylim(-10, 10) # y轴限制坐标从0到100
ax1.set_xticks([1, 1.5, 3, 4, 5, 6, 8, 10]) # x轴刻度
ax1.set_yticks([5, 10, 20, 30, 40]) # y轴刻度
ax1.set_xticklabels(['one', 'two', 'four', 'six', 'eight', 'ten', 'x', 'xx'])
ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax1.set_ylabel('line1')
ax2 = ax1.twinx()
ax2.plot([1, 3, 5, 8], [3, 7, 3, 5])
ax2.set_ylabel('line2')
plt.show()