本文目录
1.绘制正弦曲线,并设置标题、坐标轴名称、坐标轴范围
x = np.linspace(-np.pi*2, np.pi*2, 256, endpoint=True) # 创建等差一维数组
y = np.sin(x)
plt.figure() # 定义图像窗口
plt.plot(x, y) # 定义曲线
plt.xlabel('X') # 设置x坐标轴名称
plt.ylabel('Y') # 设置y坐标轴名称
plt.title("Y=Sin(X)") # 设置标题名称
plt.xlim((-np.pi*2, np.pi*2)) # 设置x轴取值范围
plt.ylim((-1, 1)) # 设置y轴取值范围
plt.show() # 画图
2.同一坐标系中绘制多种曲线并通过样式、宽度、颜色加以区分
x = np.linspace(-2*np.pi,2*np.pi,256,endpoint=True)
y1=np.sin(x)
y2=np.cos(x)
plt.figure(num=1,figsize=(5,5))
plt.plot(x,y1,label="y=sin(x)")
plt.plot(x,y2,color='red',linewidth=1.0,linestyle="--",label="y=cos(x)")
plt.xlabel('X') # 设置x坐标轴名称
plt.ylabel('Y') # 设置y坐标轴名称
plt.xlim((-np.pi*2, np.pi*2)) # 设置x轴取值范围
plt.ylim((-1, 1)) # 设置y轴取值范围
plt.legend()
plt.show()
3.绘制多轴图,即将多幅子图绘制在同一画板
x = np.linspace(-2*np.pi, 2*np.pi, 256, endpoint=True)
y1 = np.sin(x)
y2 = np.tan(x)
plt.figure() # 定义图像窗口
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 5))
axes = axes.flatten()
plt.plot(x, y1)
plt.title("y=sin(x)")
plt.xlim((-2*np.pi, 2*np.pi))
plt.ylim((-1, 1))
plt.sca(axes[0]) # 画第一个子图y=sin(x)
plt.plot(x, y2)
plt.title("y=tan(x)")
plt.xlim((-2*np.pi, 2*np.pi))
plt.ylim((-10, 10))
plt.sca(axes[1]) # 画第二个子图y=tan(x)
4.直方图的绘制(数据自己定义)
plt.figure()
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)
num_bins=50
plt.hist(x, num_bins, facecolor='blue', alpha=0.5)
plt.show()
5.绘制散点图
n = 1024 # data size
X = np.random.normal(0, 1, n) # 每一个点的X值
Y = np.random.normal(0, 1, n) # 每一个点的Y值
T = np.arctan2(Y, X) # 根据x,y分类成不同颜色
plt.scatter(X, Y, s=75, c=T, alpha=.5)
plt.xlim(-1.5, 1.5)
plt.xticks(()) # 不显示x坐标轴
plt.ylim(-1.5, 1.5)
plt.yticks(()) # 不显示y坐标轴
plt.show()
6.绘制盒状图
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
print(np.array(all_data).shape) # (3, 100)
plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()
7.尝试matplotlib库的其它功能,如2D,3D等
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y) # x-y 平面的网格
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
plt.show()
8.安装pyecharts,利用pyecharts自选数据并进行可视化展示
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.render("./render.html")
总结
使用import
导入模块matplotlib.pyplot
,并简写成plt
。
使用plt.figure
定义一个图像窗口。
使用plt.xlim
设置x坐标轴范围,使用plt.ylim
设置y坐标轴范围。
使用plt.xlabel
设置x坐标轴名称,使用plt.ylabel
设置y坐标轴名称。
使用plt.xticks
设置x轴刻度,使用plt.yticks
设置y轴刻度以及名称。
使用 plt.legend
展示图例。
使用plt.xticks()
函数来隐藏x坐标轴,使用plt.yticks()
函数来隐藏y坐标轴。
使用plt.plot
绘制折线图,plt.scatter()
绘制散点图,plt.hist()
绘制直方图,plt.boxplot()
绘制盒状图。
安装pyecharts
命令pip install pyecharts
。