1.plot
plot的数据是numpy数组
参数:
# 画单条线
plot([x], y, [fmt], *, data=None, **kwargs)
# 画多条线
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
一个简单例子
import matplotlib.pyplot as plt
import numpy as npxpoints = np.array([1, 8])
ypoints = np.array([3, 10])plt.plot(xpoints, ypoints)
plt.show()
fmt包括:
颜色字符:'b' 蓝色,'m' 洋红色,'g' 绿色,'y' 黄色,'r' 红色,'k' 黑色,'w' 白色,'c' 青绿色,'#008000' RGB 颜色符串。多条曲线不指定颜色时,会自动选择不同颜色。
线型参数:'‐' 实线,'‐‐' 破折线,'‐.' 点划线,':' 虚线。
标记字符:'.' 点标记,',' 像素标记(极小点),'o' 实心圈标记,'v' 倒三角标记,'^' 上三角标记,'>' 右三角标记,'<' 左三角标记...
import numpy as np
import matplotlib.pyplot as pltx = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.plot(x, y)plt.xlabel("x - label")
plt.ylabel("y - label")plt.show()#要下载中文字体
2.subplot
1.subplot
就是划分区域
下面这个例子就是 2x2的区域
import matplotlib.pyplot as plt
import numpy as np#plot 1:
x = np.array([0, 6])
y = np.array([0, 100])plt.subplot(2, 2, 1)
plt.plot(x,y)
plt.title("plot 1")#plot 2:
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])plt.subplot(2, 2, 2)
plt.plot(x,y)
plt.title("plot 2")#plot 3:
x = np.array([1, 2, 3, 4])
y = np.array([3, 5, 7, 9])plt.subplot(2, 2, 3)
plt.plot(x,y)
plt.title("plot 3")#plot 4:
x = np.array([1, 2, 3, 4])
y = np.array([4, 5, 6, 7])plt.subplot(2, 2, 4)
plt.plot(x,y)
plt.title("plot 4")plt.suptitle("RUNOOB subplot Test")
plt.show()
2 subplots
import matplotlib.pyplot as plt
import numpy as np# 创建一些测试数据 -- 图1
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)# 创建一个画像和子图 -- 图2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')# 创建两个子图 -- 图3 一行2列
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)# 创建四个子图 -- 图4
fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
axs[0, 0].plot(x, y)
axs[1, 1].scatter(x, y)
3.显示图像
imshow函数
imshow(X, cmap=None, norm=None, aspect=None,interpolation=None, alpha=None, vmin=None, vmax=None,
origin=None, extent=None, shape=None, filternorm=1,
filterrad=4.0, imlim=None, resample=None, url=None, *, data=None, **kwargs)
cmap
:颜色映射。用于控制图像中不同数值所对应的颜色。可以选择内置的颜色映射,如gray
、hot
、jet
等,也可以自定义颜色映射。
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image# 加载地图图像, 下载地址:https://static.jyshare.com/images/demo/map.jpeg
img = Image.open('map.jpg')# 转换为数组
data = np.array(img)# 绘制地图
plt.imshow(data)# 隐藏坐标轴
plt.axis('off')# 显示图像
plt.show()