第一步:安装库
这个自行解决,有很多方法...
开始学习!!!
导入:
import matplotlib.pyplot as plt
设置中文字体:
matplotlib.rcParams['font.family']='SimHei'
使用该库绘制一个简单的函数图像;
$$
y = log_2x^2+log_2
$$
代码如下:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
def f(x):
y = np.log(x)**2+np.log(x)
return y
x = np.arange(1,100)
y=f(x)
matplotlib.rcParams['font.family']='SimHei'
plt.title("测试")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
pyplot的绘图区域
plt.subplot(nrows, ncols, plot_number)
在全局绘图区域中创建一个分区体系,并定位到一个子绘图区域
plt.subplot(3,2,4) 简写形式:plt.subplot(324) 划分3*2个区,位于第4个绘图区
import matplotlib.pyplot as plt
import numpy as np
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)
a = np.arange(0,5,0.02)
plt.subplot(211)
plt.plot(a,f(a))
plt.subplot(212)
plt.plot(a,np.cos(2*np.pi*a),color='r', linestyle='--', marker='.') #等价于plt.plot(a,np.cos(2*np.pi*a),'r--.')
plt.savefig('例1')
plt.show()
美化
演示效果
a = np.arange(10)
plt.plot(a,a*1.5,'go:',a,a*2.5,'rx',a,a*3.5,'^',a,a*4.5,'bd-.')
plt.show()