Detailed explanation of DataFrame.plot function (5)
The fifth part mainly introduces the df.scatter, df.box and df.boxplot drawing functions, scatter plots and box plot examples. The scatter plot controls the size of the scatter points through the s parameter, and the colormap can display gradient colors.
1. scatter
DataFrame.plot.scatter(x, y, s=None, c=None, **kwargs)
c: is the color of each point, which can be a value or an array value
s: is the size of each point, Can be a value or an array value
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
[6.4, 3.2, 1], [5.9, 3.0, 2]],
columns=['length', 'width', 'species'])
ax1 = df.plot.scatter(x='length',
y='width',
c='Blue',marker='H',figsize=(3,3),fontsize=12,title='Scatter')
ax2 = df.plot.scatter(x='length',
y='width',
c='species',marker='H',figsize=(3,3),fontsize=12,title='Scatter',
colormap='viridis')
c is a list, which displays the size of the value through colormap. Scatter can represent three-dimensional data.
ax2 = df.plot.scatter(x='length',
y='width',
s=df['species']*100,marker='H',figsize=(3,3),fontsize=12,title='Scatter')
s=df['species']*100, because the value is relatively small, the magnification factor, the size of the hexagon.
2. box
DataFrame.plot.box(by=None, **kwargs)
data = np.random.randn(25, 4)
df = pd.DataFrame(data, columns=list('ABCD'))
ax = df.plot.box(figsize=(3,3),title='Box')
age_list = [8, 10, 12, 14, 72, 74, 76, 78, 20, 25, 30, 35, 60, 85]
df = pd.DataFrame({"gender": list("MMMMMMMMFFFFFF"), "age": age_list})
ax = df.plot.box(column="age", by="gender", figsize=(3, 2))
by="gender", statistics by gender
3. boxplot
DataFrame.boxplot(column=None, by=None, ax=None, fontsize=None, rot=0, grid=True, figsize=None, layout=None, return_type=None, backend=None, **kwargs)
np.random.seed(1234)
df = pd.DataFrame(np.random.randn(10, 4),columns=['Col1', 'Col2', 'Col3', 'Col4'])
boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3'],color='b')
Specify to display 3 columns of data, the effect is as follows:
df = pd.DataFrame(np.random.randn(10, 2),columns=['Col1', 'Col2'])
df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A','B', 'B', 'B', 'B', 'B'])
boxplot = df.boxplot(by='X',color='b',figsize=(6,4))
plt.show()
by='X', display according to X classification
Previous article area, pie, hist, hexbin four functions demonstrate
the next article subplot function, understand the meaning and function of fig and ax (axs)