视频教程
案例一
import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
x = np.linspace(-1,1,50)
y = 2 * x + 1
plt.plot(x,y)
plt.show()

案例二

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
x = np.linspace(-3,3,50)
y1 = 2 * x + 1
y2 = x ** 2
plt.figure()
plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--')
plt.plot(x, y2)
plt.show()
设置坐标轴

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
x = np.linspace(-np.pi,np.pi)
y = np.sin(x)
plt.figure()
plt.plot(x,y)
plt.ylim(-2,2)
plt.show()
设置中文标签

import matplotlib.pyplot as plt
import pylab as mpl
import numpy as np
if __name__ == '__main__':
x = np.linspace(-np.pi,np.pi )
y = np.tan(x)
plt.figure()
plt.plot(x,y)
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
plt.title("tan函数")
plt.xlabel("横坐标")
plt.ylabel("纵坐标")
plt.show()
图例

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi,np.pi )
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.figure()
plt.plot(x,y_sin,label='up')
plt.plot(x,y_cos,color='red',label='down')
plt.legend()
plt.title("函数")
plt.xlabel("横坐标")
plt.ylabel("纵坐标")
plt.show()
plt.legend(loc=‘upper left’) 设置位置
标注
待扩展
plt.xticks()
练习

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.random.randint(0,100,10)
y = np.random.randint(0,100,10)
plt.figure()
plt.scatter(x,y,color = 'limegreen',marker='s', label='city')
plt.legend(loc='best')
plt.title("旅行者问题")
plt.xlabel("横坐标")
plt.ylabel("纵坐标")
plt.show()
|