-
matplotlib:
-
Data can be visualized and presented more intuitively
-
Make data more objective and convincing
-
-
matplotlib architecture:
- The upper layer calls the lower layer
- Backend: implement drawing area (allocate drawing resources)
- Art: figure, axes, axis
- Script: pyplot
-
Basic drawing:
- Line chart: coordinates of points (horizontal, vertical)
- figure (draw the drawing area), plot, show (display the axes)
- xticks, yticks (set ticks, Chinese)
- xlable, ylable (set axis interpretation)
- plot(parameter) (plot coordinates)
- lengthend (set display legend)
- plt.subplots(nrows=Num,ncols=Num,figsize=(20,8)) (realize the drawing of multiple coordinate systems)
-
Histogram:
-
Number of groups: The data is divided into several groups according to different ranges
-
Group distance: the difference between the two endpoints of each group
-
API:
plt.hist(x,bins=None,normed=None,**kwargs)
plt.grid(True,linestyle='-',alpha=0.5): Display grid
-
-
Compared:
- Histogram: suitable for data where the x-coordinate is connected, the amount of data is large
- Histogram: suitable for few categories and small amount of data
-
Pie chart:
- Application scenario: Indicates the proportion of different categories
- API:plt.pie(x,explode=List,labels=None,autopct=’%1.2f%%’,colors=List,shadow=True,startangle=Num)
- Drawing: pay attention to display the whole circle plt.axis('equal')
-
K line chart
-
API:
candlestick_ochl(axes,day,width=0.2,colorup=‘r’,colordown=‘g’)
-
-
Line chart, column chart, histogram, pie chart case
- Line chart first experience
# 实现温度变化(折线图)的绘制 #1、先生成fig plt.figure(figsize=(20,8)) # 2、准备数据、折现图点,x,y坐标个数一一对应 x = range(10) y = range(10,20) plt.plot(x,y) # 3、显示调用show() plt.show()
- Line chart first experience
# 画出温度变化图 # 创建一个figure plt.figure(figsize=(20,8)) # 准备x,y坐标的数据 x = range(60) y_shanghai = [random.uniform(15,18) for i in x] # 画折现图 plt.plot(x,y_shanghai) plt.show()
- The temperatures of two cities are displayed in one coordinate system
# 显示某城市的温度变化图,11-12 # 调价刻度调节 plt.figure(figsize=(20,8)) # 准备x,y坐标的数据 x = range(60) # 准备上海y轴坐标 y_shanghai = [random.uniform(15,18) for i in x] # 准备北京的y轴坐标 y_beijing = [random.uniform(1,3) for i in x] # 设置中文刻度值 x_ch = ["11点{}分".format(i) for i in x] y_ticks = range(40) # 画折现图 plt.plot(x,y_shanghai,label='上海') plt.plot(x,y_beijing,color='r',linestyle='--',label='北京') # 修改刻度值 # 指定显示的x刻度的列表 # 第一个参数:必须是值 # 第二个参数:指定跟第一个参数对应的中文 # y 0-40 plt.xticks(x[::5],x_ch[::5],fontsize=20) plt.yticks(y_ticks[::5],fontsize=20) # 增加标题,坐标轴描述 plt.xlabel('时间',fontsize=20) plt.ylabel('温度',fontsize=20) plt.title('一些城市从11点到12点之间的温度',fontsize=20) # 增加图例的显示 plt.legend(loc='best',fontsize=20) plt.show()
- Temperatures in two cities, displayed in multiple coordinate systems
# 两个城市的温度,在多个坐标系中显示 # 调价刻度调节 # plt.figure(figsize=(20,8)) fig,ax = plt.subplots(nrows=1,ncols=2,figsize=(20,8)) # 准备数据 x = range(60) # 准备上海y轴坐标 y_shanghai = [random.uniform(15,18) for i in x] # 准备北京的y轴坐标 y_beijing = [random.uniform(1,3) for i in x] # 设置中文刻度值 x_ch = ["11点{}分".format(i) for i in x] y_ticks = range(40) # 画折现图 ax[0].plot(x,y_shanghai,label='上海') ax[1].plot(x,y_beijing,color='r',linestyle='--',label='北京') # 修改刻度值 # 指定显示的x刻度的列表 # 第一个参数:必须是值 # 第二个参数:指定跟第一个参数对应的中文 # y 0-40 ax[0].set_xticks(x[::5],x_ch[::5]) ax[1].set_xticks(x[::5],x_ch[::5]) ax[0].set_yticks(y_ticks[::5]) ax[1].set_yticks(y_ticks[::5]) # 增加标题,坐标轴描述 ax[0].set_xlabel('时间',fontsize=20) ax[0].set_ylabel('温度',fontsize=20) ax[0].set_title('一些城市从11点到12点之间的温度',fontsize=20) ax[1].set_xlabel('时间',fontsize=20) ax[1].set_ylabel('温度',fontsize=20) ax[1].set_title('一些城市从11点到12点之间的温度',fontsize=20) # 增加图例的显示 ax[0].legend(loc='best',fontsize=20) ax[1].legend(loc='best',fontsize=20) plt.show()
- Comparison of movie box office data (bar chart)
# 电影票房数据的对比(柱状图) plt.figure(figsize=(20,8)) # 电影名字,每部电影对应的票房 movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其他'] y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222] # 放进横坐标的数字列表 x = range(len(movie_name)) color = ['b','r','g','y','c','m','y','k','c','g','g'] # 使用plt.bar去显示 # plot.bar:填入的x坐标必须全是数字 plt.bar(x,y,width=0.2,color=color) # 去修改刻度,以及电影名字显示 plt.xticks(x,movie_name,fontsize=16) plt.show()
- Compare the box office of the first day and first week of different movies
# 对比不同电影的首日、首周的电影票房 # 电影票房数据的对比(柱状图) plt.figure(figsize=(20,8)) # 电影名字,每部电影对应的票房 movie_name = ['雷声3:诸神黄昏','正义联盟','寻梦环游记'] first_day = [10587.6,10062.5,1275.7] first_weekend = [36224.9,34479.6,11830] x = range(len(movie_name)) # 使用plt.bar去显示 # plot.bar:填入的x坐标必须全是数字 plt.bar(x,first_day,width=0.2,label='首日票房') plt.bar([i+0.2 for i in x],first_weekend,width=0.2,label='首周票房') # 去修改刻度,以及电影名字显示 plt.xticks([i+0.1 for i in x],movie_name,fontsize=16) # 显示图例 plt.legend(loc='best',fontsize=20) plt.show()
- Histogram of movie duration distribution
# 电影时长分布直方图 # 组数 组距 # 组距:2 # 创建 plt.figure(figsize=(20,8)) time = [131,98,125,131,139,131,117,128,108,135,138,131,102,107,114,119,128,121,142,127,130,124,124,127,138,117,121,102,130,138,137,126,128,132,131,119,114,126,115] bins = 2 # 组数 = 极差/组距 group = int((max(time)-min(time))/bins) # 画直方图 # normed参数:指定纵坐标显示频率 # plt.hist(time,group,normed=1) plt.hist(time,group) # 指定刻度的范围,以及步长 plt.xticks(list(range(min(time),max(time)))[::2]) plt.xlabel('电影市场大小',fontsize=20) plt.ylabel('电影的数据量',fontsize=20) # 添加网格 plt.grid(True,linestyle='--',alpha=0.5) plt.show()
- The percentage of films made
# 电影的拍片占比显示 plt.figure(figsize=(20,8)) movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其他'] place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105] color = ['b','r','g','y','c','m','y','k','c','g','g'] # 显示饼图 plt.pie(place_count,labels=movie_name,autopct='%1.2f%%',colors=color) plt.axis('equal') plt.legend(loc='best') plt.show()
- Pie chart showing pet preference ratio
labels = 'Frogs','Hogs','Dogs','Logs' sizes = [15,30,45,10] explode = (0,0.1,0,0) fi1,ax1 = plt.subplots() ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=True,startangle=90) ax1.axis('equal') plt.show()
Ultra-detailed matplotlib study notes (including line charts, histograms, histograms, and pie chart cases)
Guess you like
Origin blog.csdn.net/qq_45821420/article/details/108929847
Recommended
Ranking