matplotlib绘图两种方式:
#面向对象方法
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 100)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x, x)
ax.plot(x, x ** 2)
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
plt.show()
#直接使用pyplot模块
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 100)
plt.plot(x,x)
plt.plot(x,x**2)
plt.xlabel(‘x’)
plt.ylabel(‘y’)
plt.show()
#解决中文乱码
import matplotlib.pyplot as plt
import numpy as np
from pylab import mpl
mpl.rcParams[‘font.sans-serif’] = [‘simHei’] #simHei window使用
mpl.rcParams[‘axes.unicode_minus’] = False
x = np.array([1, 2, 3])
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title(‘第一张图’)
plt.plot(x, x, ‘C5’, linewidth=3, linestyle=‘solid’)
plt.show()
annotate 美[ˈænəteɪt] 注释
coords 扣奥兹 坐标
spine 边框线
gca get coordz axes的简称:获取坐标轴
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 3, 100)
plt.figure(figsize=(10, 5))
plt.plot(x, x, ‘C0’, linewidth=3, linestyle=‘solid’)
plt.plot(x, x ** 2, ‘C1’, linewidth=3, linestyle=‘solid’)
plt.title(‘t1’, fontsize=16)
plt.xlabel(‘x轴标签’, fontsize=16)
plt.ylabel(‘y轴标签’, fontsize=16)
plt.annotate(‘ y = x y=x y=x’, xy=(2, 2), fontsize=16, color=‘C0’, xytext=(+40, -40), textcoords=‘offset points’,
arrowprops=dict(arrowstyle=‘->’, connectionstyle=‘arc3,rad = .2’, color=‘C0’))
plt.annotate(‘ y = x 2 y=x^2 y=x2’, xy=(2.5, 6.25), fontsize=16, color=‘C1’, xytext=(+40, -40), textcoords=‘offset points’,
arrowprops=dict(arrowstyle=‘->’, connectionstyle=‘arc3,rad = .2’, color=‘C0’))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,3,100)
plt.figure(figsize=(10, 5))
plt.plot(x, x, ‘C0’, linewidth=3, linestyle=‘solid’, label=‘ y = x y=x y=x’)
plt.plot(x, x**2, ‘C1’, linewidth=3, linestyle=‘solid’, label=‘ y = x 2 y=x^2 y=x2’)
plt.title(‘t1’, fontsize=16)
plt.xlabel(‘x轴标签’, fontsize=16)
plt.ylabel(‘y轴标签’, fontsize=16)
plt.annotate(‘ y = x y=x y=x’, xy=(2, 2), fontsize=16, color=‘C0’, xytext=(+40, -40), textcoords=‘offset points’,
arrowprops=dict(arrowstyle=‘->’, connecti