import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1,1,10) y = x**2 plt.plot(x,y) plt.show()figure布局
figure类似一张画布
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3, 3, 50) y1 = 2 * x + 1 y2 = 2*x ** 2 + 1 y3 = x ** 3 plt.figure() #figure1 plt.plot(x, y1) plt.show() plt.figure() #figure2 plt.plot(x, y2) plt.plot(x, y3, color='red', linestyle="--") plt.show()
这里一个figure段一个画布,y1单独出现,y2y3在第二张画布
plt.plot()plot可以传入一些基本参数
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3, 3, 50) y1 = 2 * x + 1 y2 = 2*x ** 2 + 1 y3 = x ** 3 plt.figure() plt.plot(x, y1) plt.show() plt.figure() plt.plot(x, y2) plt.plot(x, y3, color='red', linestyle="--") plt.show()
如y3,可以在内部调整曲线的一些参数,如color,linestyle
axes一些方法先定义以下函数
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3, 3, 1000) y = 20 * x ** 2 + 1
加入figure
plt.figure()
plt.plot(x, y)
plt.xlim(-1, 1)
plt.ylim(0, 20)
plt.xlabel('This is x')
plt.ylabel('This is y')
这里lim对x,y轴做出限制,实际上也只是边界问题,label不言而喻.
还可以用ticks做出一些操作
new_ticks = np.linspace(-1, 2, 5) print(new_ticks) plt.xticks(new_ticks) plt.yticks([-2, -1, 1, 2, 8], ['really bad', 'bad', 'normal', 'good', 'really good'])
效果如下
这里的ticks在y轴上做出了一些描述
gca :get current axis
ax = plt.gca()
ax.spines['right'].set_color('none')
plt.show()
如上图,这里就是把右边框架去掉



