Python数据可视化是将数据以图形或者表格的形式展示
这里会使用到matplotlib这个库
1.点图和线图
plt.plot(x,y,format_string,**kwargs) #绘制点图或者线图
**kwargs 是第二组xy
plt.subplot(rows,columns,plotNum) #生成子图
生成一个rows行columns列的绘图区,plotNum表示位于哪个绘图区,1<=plotNum<=rows*columns
subplot(1,2,1) #也可以写成 subplot(121)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname="./msyh.ttf")
# plt.rcParams['font.sans-serif']=['SimHei']
#生成X
x1 = np.linspace(0,5) #生成50个点,间隔是均匀的
x2 = np.linspace(0,2) #也是生成50个点
#生成Y
y1 = np.cos(2*np.pi*x1)*np.exp(-x1)
y2 = np.cos(2*np.pi*x2)
#生成第一个子图
plt.subplot(2,1,1) #表示一共有两个子图,该子图位于第一行的第一列
#绘图
plt.plot(x1,y1,'yo-')
plt.title("绘制点线图",fontproperties=font)
plt.ylabel("阻尼振动图",fontproperties=font)
#绘制第二个图
plt.subplot(2,1,2)
plt.plot(x2,y2,"r.-")
plt.xlabel("时间(s)",fontproperties=font)
plt.ylabel("非阻尼",fontproperties=font)
plt.show() #出图
使用matplotlib绘制点线图
在这里说一下,在matplotlib中显示中文的两种方法:
第一种:
plt.rcParams['font.sans-serif']=['SimHei']
第二种:
from matplotlib.font_manager import FontProperties
font = FontProperties(fname="./msyh.ttf") #msyh.tff是我在系统字体库中找到的微软雅黑字体,我将它拷贝了一份放到代码同级目录下。
之后,凡是显示中文的地方都要加上 fontproperties=font 这个参数:
plt.xlabel("时间(s)",fontproperties=font)
plt.ylabel("非阻尼",fontproperties=font)
2.直方图
n,bins,patches = hist(
arr,
bins=10,
normed=0,
facecolor="black",
edgecolor