Detailed explanation of DataFrame.plot function (4)

Detailed explanation of DataFrame.plot function (4)

The fourth part mainly introduces the use of four drawing functions: df.area, df.pie, df.hist, and df.hexbin. Among them, the characteristic parameters bins and by of the histogram are examples, and the gridsize parameter of hexbin is used.

1. area

DataFrame.plot.area(x=None, y=None, stacked=True, **kwargs)

df = pd.DataFrame({
    'sales': [3, 2, 3, 9, 10, 6],
    'signups': [5, 5, 6, 12, 14, 13],
    'visits': [20, 42, 28, 62, 81, 50],
}, index=pd.date_range(start='2020/01/01', end='2020/07/01',freq='M'))
ax = df.plot.area(figsize=(4,3),title='Area stacked')
ax = df.plot.area(figsize=(4,3),stacked=False,title='Area ')

The area chart is stacked by default, stacked=True

Insert image description here

2. pie

DataFrame.plot.pie(**kwargs)


df = pd.DataFrame({'mass':abs(np.random.randn(4)),
                   'radius': abs(np.random.randn(4))},
                  index=['A', 'B', 'C','D'])
plot = df.plot.pie(y='mass', figsize=(4, 4))

Insert image description here
Subgraph example:

plot = df.plot.pie(subplots=True, figsize=(8, 4))

Insert image description here

3. hist

DataFrame.plot.hist(by=None, bins=10, **kwargs)
bins: number of histogram bins, default 10

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

df = pd.DataFrame({'a': np.random.randn(100) + 0.5,
        'b': np.random.randn(100),
        'c': np.random.randn(100) - 0.5},
        columns =['a', 'b', 'c'])

df.plot.hist(figsize=(6,4),alpha = 0.5,bins=20)
plt.show()

Insert image description here

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.hist(column=["age"], by="gender", figsize=(8, 4))

by="gender", sort by gender

Insert image description here

4. hexbin

DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs)

n = 10000
df = pd.DataFrame({'x': np.random.randn(n),
                   'y': np.random.randn(n)})
ax = df.plot.hexbin(x='x', y='y', gridsize=20)
plt.show()

gridsize: The default is the number of hexagons in the x direction, the default value is 100.
The corresponding number of hexagons in the y-direction is chosen in such a way that the hexagons approximate the rules, as can be seen in the second picture, where the hexagons are deformed. gridsize can be a tuple with two elements specifying the number of hexagons in the x and y directions respectively.

Insert image description here

ax = df.plot.hexbin(x='x', y='y', gridsize=(30,20))
plt.show()

Insert image description here
Previous article bar and barh functions, three parameters rot, alpha, and stackedNext
article scatter, box function, and related parameters

Guess you like

Origin blog.csdn.net/qq_39065491/article/details/132491389