目录
一、Basic Usage
a simple example
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
fig, ax = plt.subplots() # Create a figure containing a single axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]); # Plot some data on the axes.
plt.show()
Parts of a Figure
tick --- 标签 legend --- 图例 Grid --- 网格 mark --- 标记 scatter plot --- 散点图
Coding styles 编码风格
OO-style 面向对象 这个代码风格用的更多
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100) # Sample data.
# Note that even in the OO-style, we use `.pyplot.figure` to create the Figure.
fig, ax = plt.subplots(figsize=(10,5))
# 绘制三条线 线性 平方 立方
ax.plot(x, x, label='linear') # Plot some data on the axes.
ax.plot(x, x**2, label='quadratic') # Plot more data on the axes...
ax.plot(x, x**3, label='cubic') # ... and some more.
ax.set_xlabel('x label') # Add an x-label to the axes.
ax.set_ylabel('y label') # Add a y-label to the axes.
ax.set_title("Simple Plot") # Add a title to the axes.
ax.legend() # Add a legend.
plt.show()
the pyplot-style
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100) # Sample data.
plt.figure(figsize=(10,5))
plt.plot(x, x, label='linear') # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic') # etc.
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()
图的 标注
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,8))
x = np.arange(0.0, 3, 0.01)
y1 = np.cos(2 * np.pi * x) # y = cos(2πx)
y2 = np.sin(2 * np.pi * x)
plt.plot(x,y1, c = 'r',label = 'cos')
plt.plot(x,y2, c = 'g',label = 'sin')
plt.ylim(-2,2)
plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.legend()
plt.show()
多图绘制
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
fig, axd = plt.subplot_mosaic([['upleft', 'right'],
['lowleft', 'right']],figsize=(10,8))
axd['upleft'].set_title('upleft')
axd['lowleft'].set_title('lowleft')
axd['right'].set_title('right')
plt.show()
二、不同类型的图像绘制
1、基础类型 basic
都是选择我遇见较多的类型
Basic plot types, usually y versus(与、与...相比) x.
①、线图
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
x = np.linspace(0, 10, 100)
y = 4 + 2 * np.sin(2 * x)
# plot
fig, ax = plt.subplots()
ax.plot(x, y, linewidth=2.0)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
②、散点图
import matplotlib.pyplot as plt
import numpy as np
# make the data
np.random.seed(3)
x = 4 + np.random.normal(0, 2, 24)
y = 4 + np.random.normal(0, 2, len(x))
# size and color:
sizes = np.random.uniform(15, 80, len(x))
colors = np.random.uniform(15, 80, len(x))
# plot
fig, ax = plt.subplots()
ax.scatter(x, y, s=sizes, c=colors, vmin=0, vmax=100) #散点图绘制
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
③、柱状图 bar
bar(x, height) / barh(y, width)
import matplotlib.pyplot as plt
import numpy as np
# make data:
np.random.seed(3)
x = 0.5 + np.arange(8)
y = np.random.uniform(2, 7, len(x))
#plot
fig,ax = plt.subplots(2,1)
ax[0].set_title('bar')
ax[0].bar(x, y, width=1, edgecolor="white", linewidth=0.7)
ax[1].set_title('barh')
ax[1].barh(x, y, edgecolor="white", linewidth=0.7)
plt.show()
④、直方图、hist(x)
opencv 图像处理里的灰度直方图
import matplotlib.pyplot as plt
import numpy as np
# make data
np.random.seed(1)
x = 4 + np.random.normal(0, 1.5, 200)
# plot:
fig, ax = plt.subplots()
ax.hist(x, bins=8, linewidth=0.5, edgecolor="white")
ax.set_title('hist')
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 56), yticks=np.linspace(0, 56, 9))
plt.show()
⑤、饼图 pie(x)
import matplotlib.pyplot as plt
import numpy as np
# make data
x = [1, 2, 3, 8]
colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(x)))
# plot
fig, ax = plt.subplots()
ax.pie(x, colors=colors, radius=3, center=(4, 4),
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()