关于使用matplotlib画图的基础操作

1·基础的折线图

import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(4,3))
plt.plot(data)
plt.show()

在这里插入图片描述

2·添加子图

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.plot(data1,color='b')
ax2.plot(data2,color='r')
ax3.plot(data3,color='r')
ax4.plot(data4,color='r')
plt.show()

在这里插入图片描述

3·识别中文及负号

plt.rcParams['font.sans-serif'] = ['SimHei'] #设置字体 以便识别中文
# plt.rcParams['font.size'] = 24   #设置字体
plt.rcParams['axes.unicode_minus'] = False #当字体是中文的时候,显示负号

4·直方图

fig = plt.figure(figsize=(4,3))
plt.hist(data,10)  #数据 和 分箱数(频数)
plt.legend('注释')
plt.grid(axis='y')
plt.title('直方图')
plt.xlabel('x轴')
plt.ylabel('y轴')
# plt.xticks()  #x轴坐标
# plt.yticks()  #y轴坐标
plt.show()

在这里插入图片描述

5·散点图

fig = plt.figure(figsize=(4,3))
plt.scatter(x,y) #x轴数据 和y轴数据
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.show()

在这里插入图片描述

6·条形图

plt.rcParams['font.size'] = 20   #设置字体
fig = plt.figure(figsize=(8,5))
plt.bar(x = fz.index.values,height=fz['price'].values,width=0.5)
# plt.legend('注释')
plt.title('条形图')
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.show()

在这里插入图片描述

7·饼图

plt.pie(fz,autopct='%2.2f%%',shadow=True,labels=fz.index)
plt.show()

在这里插入图片描述

8·箱型图

fig = plt.figure(figsize = (4,3))
plt.boxplot(data.iloc[:-1,0])
plt.xlabel('比利时')

在这里插入图片描述

发布了49 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44166997/article/details/99302230