matplotlib绘图小结(数据可视化,数据分析)

matplotlib是python的一个绘图库

主要画图框架结构:

三层结构:

       1)容器层

               画板层(Canvas)

               画布层(Figure)

               绘图区(Axes)/坐标系(axis),子图(subplots)

       2)辅助显示层

                刻度线(xticks、yticks),图例(legend),标签(xlabel、ylabel),标题(title),网格(grid)......

       3)图像层

                    图像显示(show)与保存(savefig)

主要步骤:

1.创建画布:plt.figure()

2.绘制图像:plt.plot()等

3.显示图像:plt.show()

代码实战(例子):

我们先来看一个简单的例子(折线图):

# 导入绘图库
import matplotlib.pyplot as plt
# 准备数据
x = [2,0,4]
y = [5,3,2]
# 创建画布
plt.figure()
# 绘制图像
plt.plot(x,y)
# 显示图像
plt.show()

保存图片:

import matplotlib.pyplot as plt

# 创建画布
plt.figure()
# 绘制图像
plt.plot([2,0,4],[5,3,2])
# 保存图像
plt.savefig("save1.png")

这样就可以看到刚刚绘制的这张图被保存在了当前目录下。

我们更改数据再绘制一张图(比如:11点到12点温度随时间的变化):

import matplotlib.pyplot as plt
import random

# 1、准备数据
x = range(60)
y = [random.uniform(15,18) for i in x]
# 2、创建画布
plt.figure(figsize=(20,8),dpi=80)
# 3、绘制图像
plt.plot(x,y)
# 4、显示图像
plt.show()

现在我们给它加上辅助信息:

import matplotlib.pyplot as plt
import random

# 1、准备数据
x = range(60)
y = [random.uniform(15,18) for i in x]
# 2、创建画布
plt.figure(figsize=(20,8),dpi=80)
# 3、绘制图像
plt.plot(x,y)
# 添加网格
plt.grid(linestyle="--",alpha=0.5)
# 添加标题
plt.xlabel("time change")
plt.ylabel("temperature change")
plt.title("temperature change from 11:00 to 12:00")
# 添加图例
plt.legend('T')
# 4、显示图像
plt.show()

更改刻度线:

import matplotlib.pyplot as plt
import random

# 1、准备数据
x = range(60)
y = [random.uniform(15,18) for i in x]
# 2、创建画布
plt.figure(figsize=(20,8),dpi=80)
# 3、绘制图像
plt.plot(x,y)
# 添加网格
plt.grid(linestyle="--",alpha=0.5)
# 添加标题
plt.xlabel("time change")
plt.ylabel("temperature change")
plt.title("temperature change from 11:00 to 12:00")
# 添加图例
plt.legend('T')
# 修改x,y刻度
plt.xticks(x[::5])
plt.yticks(range(0,40,5))
# 4、显示图像
plt.show()

接下来我们在同一张图中利用多组数据绘制:

import matplotlib.pyplot as plt
import random

# 1、准备数据
x = range(60)
y_1 = [random.uniform(15,18) for i in x]
y_2 = [random.uniform(1,3) for i in x]
# 2、创建画布
plt.figure(figsize=(20,8),dpi=80)
# 3、绘制图像
#plt.plot(x,y_1)
plt.plot(x,y_1,color="r",linestyle="--",label="shanghai")  #颜色:r红色 g绿色 w白色 c青色 m洋红 y黄色 k黑色  风格字符:-实线 --虚线 -.点划线 :点虚线 ''留空、空格
plt.plot(x,y_2,label="bejing")   # label和图例legend()同时使用
# 显示图例
plt.legend()
# 添加网格
plt.grid(linestyle="--",alpha=0.5)
#添加标题
plt.xlabel("time change")
plt.ylabel("temperature change")
plt.title("temperature change from 11:00 to 12:00")
# 修改x,y刻度
plt.xticks(x[::5])
plt.yticks(range(0,40,5))
# 5、显示图像
plt.show()

使用子图,同时绘制多个图:

import matplotlib.pyplot as plt
import random

# 1、准备数据
x = range(60)
y_1 = [random.uniform(15,18) for i in x]
y_2 = [random.uniform(1,3) for i in x]
# 2、创建画布
figure,axes = plt.subplots(nrows=1,ncols=2,figsize=(20,8),dpi=80)
# 3、绘制图像
#plt.plot(x,y_1)
axes[0].plot(x,y_1,color="r",linestyle="--",label="shanghai")  #颜色:r红色 g绿色 w白色 c青色 m洋红 y黄色 k黑色  风格字符:-实线 --虚线 -.点划线 :点虚线 ''留空、空格
axes[1].plot(x,y_2,label="bejing")   # label和图例legend()同时使用
# 显示图例
axes[1].legend()
axes[0].legend()
# 修改x,y刻度
x_label = ["11h:{}m".format(i) for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_label)
axes[0].set_yticks(range(0,40,5))
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_label)
axes[0].set_yticks(range(0,40,5))
# 添加网格
axes[0].grid(linestyle="--",alpha=0.5)
axes[1].grid(linestyle="--",alpha=0.5)
# 添加描述信息
axes[0].set_xlabel("time change")
axes[0].set_ylabel("temperature change")
axes[0].set_title("temperature change from 11:00 to 12:00")

axes[1].set_xlabel("time change")
axes[1].set_ylabel("temperature change")
axes[1].set_title("temperature change from 11:00 to 12:00")
# 5、显示图像
plt.show()

我们再看看一些其它的图:

1、散点图

import matplotlib.pyplot as plt
import numpy as np

# 准备数据
x = np.random.uniform(10,100,60)
y = np.random.uniform(10,100,60)
## 画布
plt.figure(figsize=(20,8),dpi=80)
# 绘图
plt.scatter(x,y)
# 显示
plt.show()

2、直方图和条形图

import matplotlib.pyplot as plt

# 准备数据
movie_names = ['fd','dfd','gjhgj']
y1 = [4043,2499,8926]
y2 = [5214,7481,6415]
# 画布
plt.figure(figsize=(20,8),dpi=80)
# 绘图
x = range(len(movie_names))
plt.bar(x,y1,width=0.2,label='ajgh')
plt.bar([i+0.2 for i in x],y2,width=0.2,label='gfg')
# 显示图例
plt.legend()
# 修改刻度
plt.xticks([i+0.1 for i in x],movie_names)
# 显示
plt.show()

import matplotlib.pyplot as plt

# 准备数据
time = [131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

# 画布
plt.figure(figsize=(20,8),dpi=80)

# 绘图
distance = 2
group_num = int((max(time) - min(time)) / distance)

plt.hist(time,bins=group_num)
# 修改刻度
plt.xticks(range(min(time),max(time)+2,distance))
# 显示
plt.show()

3、饼状图

import matplotlib.pyplot as plt

# 准备数据
movie_name = ['Raytheon 3 Twilight of the gods','alliance of justice','murder of the Orient Express','journey to the dream circle','global storm','legend of conquering demons','pursuit','seventy-seven days','secret war','crazy beast','others']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]
 
# 创建画布,绘制饼图,添加标题/坐标轴刻度标签
plt.figure(figsize=(18, 6), dpi=80)

plt.pie(place_count, labels=movie_name, autopct="%1.2f%%")

plt.axis("equal")  #  坐标轴长宽相等,保证饼图成圆形
 
plt.title("Proportion of film arrangement")
plt.legend()
plt.show()

猜你喜欢

转载自blog.csdn.net/qq_41750911/article/details/118463329