Python visualization 53|pandas one line of code drawing

table of Contents

1. Single group of line chart

2. Multiple groups of line graphs

3. A single group of bar graphs

4. Multiple groups of bar graphs

5. Stacked bar chart

6. Horizontal stacked bar chart

7. Histogram

8. Faceted histogram

9. Box plot

10. Area chart

11. Stacked area chart

12. Scatter chart

13, a single group of pie charts

14. Multiple groups of pie charts

15. Faceted diagram

16, hexbin diagram

17、andrews_curves图

18. Nuclear density map

19、parallel_coordinates图

20、autocorrelation_plot图

21, radviz diagram

22, bootstrap_plot graph

23. Subplot

24. Arbitrary arrangement of sub-pictures

25. Draw a data table in the graph

27. More pandas visualization and refinement materials


Pandas visualization mainly relies on the following two functions:

  • pandas.DataFrame.plot

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html?highlight=plot#pandas.DataFrame.plot

  • pandas.Series.plot

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.plot.html?highlight=plot#pandas.Series.plot

The following graphs can be drawn, pay attention to the subtle differences between Dataframe and Series:'area','bar','barh','box','density','hexbin','hist','kde','line' ,'pie','scatter'

Import dependent packages

import matplotlib.pyplot as plt 
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
plt.style.use('dark_background')#设置绘图风格

1. Single group of line chart

np.random.seed(0)#使得每次生成的随机数相同
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
ts1 = ts.cumsum()#累加
ts1.plot(kind="line")#默认绘制折线图

 

2. Multiple groups of line graphs

np.random.seed(0)
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.cumsum()
df.plot()#默认绘制折线图

 

3. A single group of bar graphs

df.iloc[5].plot(kind="bar")

4. Multiple groups of bar graphs

df2 = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df2.plot.bar()

5. Stacked bar chart

df2.plot.bar(stacked=True)

6. Horizontal stacked bar chart

df2.plot.barh(stacked=True)

7. Histogram

df4 = pd.DataFrame(
    {
        "a": np.random.randn(1000) + 1,
        "b": np.random.randn(1000),
        "c": np.random.randn(1000) - 1,
    },
    columns=["a", "b", "c"],
)
df4.plot.hist(alpha=0.8)

8. Faceted histogram

df.diff().hist(color="r", alpha=0.9, bins=50)

9. Box plot

df = pd.DataFrame(np.random.rand(10, 5), columns=["A", "B", "C", "D", "E"])
df.plot.box()

10. Area chart

df = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df.plot.area()

11. Stacked area chart

df.plot.area(stacked=False)

12. Scatter chart

ax = df.plot.scatter(x="a", y="b", color="r", label="Group 1",s=90)
df.plot.scatter(x="c", y="d", color="g", label="Group 2", ax=ax,s=90)

13, a single group of pie charts

series = pd.Series(3 * np.random.rand(4), index=["a", "b", "c", "d"], name="series")
series.plot.pie(figsize=(6, 6))

14. Multiple groups of pie charts

df = pd.DataFrame(
    3 * np.random.rand(4, 2), index=["a", "b", "c", "d"], columns=["x", "y"]
)
df.plot.pie(subplots=True, figsize=(8, 4))

15. Faceted diagram

import matplotlib as mpl
mpl.rc_file_defaults()
plt.style.use('fivethirtyeight')
from pandas.plotting import scatter_matrix
df = pd.DataFrame(np.random.randn(1000, 4), columns=["a", "b", "c", "d"])
scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal="kde")
plt.show()

16, hexbin diagram

df = pd.DataFrame(np.random.randn(1000, 2), columns=["a", "b"])
df["b"] = df["b"] + np.arange(1000)
df.plot.hexbin(x="a", y="b", gridsize=25)

17、andrews_curves图

from pandas.plotting import andrews_curves
mpl.rc_file_defaults()
data = pd.read_csv("iris.data.txt")
plt.style.use('dark_background')
andrews_curves(data, "Name")

18. Nuclear density map

ser = pd.Series(np.random.randn(1000))
ser.plot.kde()

19、parallel_coordinates图

from pandas.plotting import parallel_coordinates
data = pd.read_csv("iris.data.txt")
plt.figure()
parallel_coordinates(data, "Name")

20、autocorrelation_plot图

from pandas.plotting import autocorrelation_plot
plt.figure();
spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000)
data = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing))
autocorrelation_plot(data)

21, radviz diagram

from pandas.plotting import radviz
data = pd.read_csv("iris.data.txt")
plt.figure()
radviz(data, "Name")

22, bootstrap_plot graph

from pandas.plotting import bootstrap_plot
data = pd.Series(np.random.rand(1000))
bootstrap_plot(data, size=50, samples=500, color="grey")

23. Subplot

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df.plot(subplots=True, figsize=(6, 6))

24. Arbitrary arrangement of sub-pictures

df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False)

fig, axes = plt.subplots(4, 4, figsize=(9, 9))
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.plot(subplots=True, ax=target1, legend=False, sharex=False, sharey=False);
(-df).plot(subplots=True, ax=target2, legend=False, sharex=False, sharey=False)

25. Draw a data table in the graph

from pandas.plotting import table
mpl.rc_file_defaults()
#plt.style.use('dark_background')
fig, ax = plt.subplots(1, 1)
table(ax, np.round(df.describe(), 2), loc="upper right", colWidths=[0.2, 0.2, 0.2]);
df.plot(ax=ax, ylim=(0, 2), legend=None);

27. More pandas visualization and refinement materials

The Plotting docs.

Make Matplotlib look like R

Setting x-axis major and minor labels

Plotting multiple charts in an IPython Jupyter notebook

Creating a multi-line plot

Plotting a heatmap

Annotate a time-series plot

Annotate a time-series plot #2

Generate Embedded plots in excel files using Pandas, Vincent and xlsxwriter

Boxplot for each quartile of a stratifying variable

 

Guess you like

Origin blog.csdn.net/qq_21478261/article/details/114744113