Detailed explanation of DataFrame.plot function (2)
The second part is mainly about the use of df.plot.line function, and also demonstrates the use of main parameters, such as style, marker, color, linewidth, markersize, grid, xlim, ylim, loc, subplot and other parameter demonstrations.
1. Line
1.1 Main parameters
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.Series(abs(np.random.randn(10)), index = pd.date_range('2020-01-01', periods = 10))
df.plot.line(style= ':',marker='H',color='b',linewidth=2,markersize=10,grid=True,figsize=(6,4),label='Label show',title='Line show parameter')
plt.legend(loc='upper left')
plt.show()
style= ':' dashed line
marker='H' mark hexagon
color='b' line blue
linewidth=2 line thickness 2
markersize=10 mark size
grid=True use grid
figsize=(6,4) icon size
label='Label show' Illustration
title='Line show parameter'Illustration title
plt.legend(loc='upper left') Illustration position
The effect is as follows:
1.2 Graphing multiple sets of data
The above is a plot of a set of data in pd.Series, and the following is a plot of multiple sets of data, with different parameters for the lines set respectively.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({'A':abs(np.random.randn(10)), 'B':abs(np.random.randn(10))},index = pd.date_range('2020-01-01', periods = 10))
df.A.plot.line(style= ':',marker='H',color='b',linewidth=2,markersize=10,grid=True,figsize=(6,4),label='Label A',title='Line show parameter')
df.B.plot.line(style= '-',marker='D',color='r',linewidth=3,markersize=10,grid=True,figsize=(6,4),label='Label B',title='Line show parameter')
plt.legend(loc='upper right')
plt.show()
The effect is as follows:
1.3 Secondary parameters
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame(abs(np.random.randn(10,2)), columns=['A','B'])
df.A.plot.line(style= ':',marker='H',color='b',figsize=(6,4),label='Label A',xlim=-2,ylim=-2,rot=30,title='Line show parameter')
df.B.plot.line(style= '-',marker='D',color='r',label='Label B',xlim=-1,ylim=-1,rot=30)
plt.legend(loc='upper right')
plt.show()
xlim=-1 x-axis minimum value
ylim=-1 y-axis minimum value
rot=30 coordinates indicate the rotation angle
Note: The parameters last executed by plot take effect.
A.plot xlim=-2,ylim=-2
B.plot xlim=-1,ylim=-1 takes effect
The effect is as follows:
adjust the size of the coordinate axis
xticks=range(20) x-axis 20 units
yticks=range(6) y-axis 6 units
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame(abs(np.random.randn(10,2)), columns=['A','B'])
df.A.plot.line(style= ':',marker='H',color='b',figsize=(6,4),label='Label A',title='Line show parameter')
df.B.plot.line(style= '-',marker='D',color='r',label='Label B',xticks=range(20),yticks=range(6))
plt.legend(loc='upper right')
plt.show()
table=True is used to set the table
fontsize=12 character size
marker='H' does not support list, such as ['H','D'], it can only be one marker, and you need to set the markers separately in each series to be effective.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame(abs(np.random.randn(10,2)), columns=['A','B'])
df.plot.line(style= [':','-.'],marker='H',color=['r','b'],table=True,fontsize=12,title='Line show parameter')
plt.legend(loc='upper right')
plt.show()
1.4 Subgraph
df = pd.DataFrame(abs(np.random.randn(10,4)), columns=['A','B','C','D'])
df.plot(subplots=True, figsize=(5, 4))
plt.show()
figsize=(5, 4) The overall image size, that is, the overall size of the 4 sub-images.
The effect is as follows:
df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=True,sharey=True)
plt.show()
layout=(2, 3) subgraph arrangement, two rows, three in each column
sharex=True shares the X-axis, the first column, only one X-axis label
sharey=True shares the Y-axis, the first row, only one Y-axis label
In contrast, when the XY axis is not shared, no sharing is the default value.
Each subplot has labels for the XY axis.
1.5 Complex subgraph
df = pd.DataFrame(abs(np.random.randn(10,4)), columns=['A','B','C','D'])
fig, axes = plt.subplots(4, 4, figsize=(9, 9)) # 图示9*9大小,4行4列
plt.subplots_adjust(wspace=0.5, hspace=0.5) #水平和垂直间距
target1 = [axes[0][0], axes[1][1], axes[2][2], axes[3][3]] # 有图的矩阵位置
target2 = [axes[3][0], axes[2][1], axes[1][2], axes[0][3]] # 有图的矩阵位置
#df和df*-1 ,两个dataframe ,分别对应到子图矩阵中的位置,对比A两个的图示
df.plot(subplots=True, ax=target1, legend=True, sharex=False, sharey=False);
(-df).plot(subplots=True, ax=target2, legend=True, sharex=False, sharey=False,title='subplot set matrix');
plt.show()
The effect is as follows:
Detailed explanation of functions in the previous article (1) Basic plot parameters Next article
Detailed explanation of functions (3) df.bar and df.barh functions, effects of rot, alpha, and stacked parameters