Matplotlib-- pattern advanced applications

Drawing the axes fig.add_subplot, ax.twinx

And two sets of variables with a variable corresponding to first establish the coordinates ax, drawn on the coordinate fold line, and the establishment of a second with the x-axis ordinate use ax.twinx (), then set the polyline data
Note:
1, do not legend loc same parameter value set Legend (), it will be blocked
2, when the x-axis labels want to rotate, before the establishment of a certain coordinate in the second set
3, two ordinate axes

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl

#防止中文乱码和图形中文显示
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

#读取数据,
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
print(data)
#将数据重新设置索引分组
data1=data.groupby('顺序').mean()

#创建图形
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(data1.index,data1.1,label='属性1',color='green')
ax1.set_ylabel('不同属性1')
plt.legend(loc='upper left')
#横坐标显示的设置一定要在建立双坐标轴之前
plt.xticks(data1.index,rotation=45)
ax2=ax1.twinx()
ax2.set_ylabel('不同属性2')
ax2.plot(data1.index,data1.2,label='属性2',color='red')
plt.legend(loc='upper right')
plt.show()

Here Insert Picture Description

Graphics combined subplot () use

plt.subplot ( 121 ) represented, which comprises a row of two pattern images (that is, two side by side in FIG.), the current image is the first
to use plt.figure (figsize = ()) to create an image of a placement space, then use plt.subplot () set Photos location

#防止中文乱码和图形中文显示
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

#读取数据,不修改
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
print(data)
#写出subset和列表参数,并修改原数据
data.dropna(subset=['顺序'],inplace=True)

#创建图形
plt.figure(figsize=(16,9))
#一行两列的第一个图
plt.subplot(121)
plt.plot(data.顺序,data.1,color='c',label='属性1的')
plt.xlabel('日期变化',fontsize=15,labelpad=20)
plt.ylabel('属性1的值',fontsize=15,labelpad=20)
plt.xticks(range(0,12,2),data.iloc[range(0,12,2),1],rotation=45)

#一行两列的第二个
plt.legend(loc='upper right',fontsize=15,)
plt.subplot(122)
plt.bar(x=data.顺序,height=data.2,color='r',label='属性2的')
plt.legend(loc='upper right',fontsize=15,)
plt.xticks(rotation=45)

plt.show()

Here Insert Picture Description

subplot2grid(shape,loc,colspan,rowspan)

shape: grid layout
loc: pattern starting position
colspan: across several columns
rowspan: span several rows
interpretation of grid positions:
** plt.subplot2grid ((2,3), (0,1), 2 = colspan, rowspan = 1) image display position denoted by the red line position shown in the figure below
represents: two rows and three columns of the grid image is placed, from the
start (0,1) ** position, across the two rows 1
Here Insert Picture Description

#防止中文乱码和图形中文显示
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

#读取数据,不修改
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
print(data)
#写出subset和列表参数,并修改原数据
data.dropna(subset=['顺序'],inplace=True)

#创建图形
plt.figure(figsize=(16,9))

#两行三列,当前图跨一行两列,网格从0,0开始
plt.subplot2grid((2,3),(0,0),colspan=1,rowspan=2)
plt.plot(data.顺序,data.2,color='r',label='属性0的')
plt.xlabel('x轴0',fontsize=15,labelpad=15)
plt.ylabel('y轴0',fontsize=15,labelpad=15)
plt.title('标题0',fontsize=20,pad=20)
plt.xticks(range(0,12,2),data.iloc[range(0,12,2),1],rotation=45)
# plt.legend(loc='upper right',fontsize=15,)

plt.subplot2grid((2,3),(0,1),colspan=2,rowspan=1)
plt.bar(x=data.顺序,height=data.1,color='r')

plt.subplot2grid((2,3),(1,1),colspan=2,rowspan=1)
plt.hist(x=data.3,bins=12,color='r')

plt.suptitle('不同图形展示',size=20)
plt.show()

Here Insert Picture Description

Published 70 original articles · won praise 1 · views 2412

Guess you like

Origin blog.csdn.net/weixin_43794311/article/details/105133372