Pandas一行代码绘制26种美图
![](https://filescdn.proginn.com/35d9f82fcbb685651fc9977424fe6aef/6a9bf74d2a49e328d8eeb2365b46ccad.webp)
1、单组折线图
2、多组折线图
3、单组条形图
4、多组条形图
5、堆积条形图
6、水平堆积条形图
7、直方图
8、分面直方图
9、箱图
10、面积图
11、堆积面积图
12、散点图
13、单组饼图
14、多组饼图
15、分面图
16、hexbin图
17、andrews_curves图
18、核密度图
19、parallel_coordinates图
20、autocorrelation_plot图
21、radviz图
22、bootstrap_plot图
23、子图(subplot)
24、子图任意排列
25、图中绘制数据表格
27、更多pandas可视化精进资料
pandas可视化主要依赖下面两个函数:
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
可绘制下面几种图,注意Dataframe和Series的细微差异:'area', 'bar', 'barh', 'box', 'density', 'hexbin', 'hist', 'kde', 'line', 'pie', 'scatter'导入依赖包
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
plt.style.use('dark_background')#设置绘图风格
1、单组折线图
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")#默认绘制折线图
![](https://filescdn.proginn.com/97fd61565bfe5d31ac995b224fe6a534/8da99e2217bbb005ed83e70f77a82e60.webp)
2、多组折线图
np.random.seed(0)
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.cumsum()
df.plot()#默认绘制折线图
![](https://filescdn.proginn.com/80235f5086c5cf3f9fcc31f86b16a7af/bed4614b9f49d94d4827fca10518ae44.webp)
3、单组条形图
df.iloc[5].plot(kind="bar")
![](https://filescdn.proginn.com/83c550b8a4aabf4300f6112cb7ee254b/f9ba5ef53eff8ed35fc98dddaaa6960f.webp)
4、多组条形图
df2 = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df2.plot.bar()
![](https://filescdn.proginn.com/58951c0e6b26ff67349eceeb6aaa6ed5/c4ee973d79f0713c897d2b60ab726423.webp)
5、堆积条形图
df2.plot.bar(stacked=True)
![](https://filescdn.proginn.com/a814045a074f747eb6e8211292707c5f/b9933a95460be291454aee60a2382e30.webp)
6、水平堆积条形图
df2.plot.barh(stacked=True)
![](https://filescdn.proginn.com/6b2a074d4497290bf0b72299afaed83c/eed0d72fe94c37aa08ed518f6f353146.webp)
7、直方图
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)
![](https://filescdn.proginn.com/d7995a2709c26e0a4a440c58ed701e08/b39292c917d447a57a18692a1a08efd7.webp)
8、分面直方图
df.diff().hist(color="r", alpha=0.9, bins=50)
![](https://filescdn.proginn.com/bdb1be2f93f27e40a0918e12468b22d2/b97fb74a14ee7b20d2cf6a14bc5af62e.webp)
9、箱图
df = pd.DataFrame(np.random.rand(10, 5), columns=["A", "B", "C", "D", "E"])
df.plot.box()
![](https://filescdn.proginn.com/0b0c10d1c4db3510128100341de29bed/f8ec1eaefe20ffe4ef9f5f750648bde4.webp)
10、面积图
df = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df.plot.area()
![](https://filescdn.proginn.com/e0f48f01bcc5e3a7d2aed61d751355a1/1b5c19ad6506ae5d755e0d7fa16bac25.webp)
11、堆积面积图
df.plot.area(stacked=False)
![](https://filescdn.proginn.com/6f83d618f023f922138fa2eaa3e7088a/e7d31a2fad8c03d19312a4856a30e766.webp)
12、散点图
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)
![](https://filescdn.proginn.com/369e23a19dd9270355aa05925f6bc362/19aa6d9c123045df0dc2a73f3c31c897.webp)
13、单组饼图
series = pd.Series(3 * np.random.rand(4), index=["a", "b", "c", "d"], name="series")
series.plot.pie(figsize=(6, 6))
![](https://filescdn.proginn.com/28a5161b815c34d19c6eec399e7dd681/61dfa0b719303633bde3604fed0b96e9.webp)
14、多组饼图
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))
![](https://filescdn.proginn.com/e71bb62523532b0d02333f4cb669ab3c/f404e8ae936b28b42ca851531ed72cb0.webp)
15、分面图
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()
![](https://filescdn.proginn.com/6637476be14a3063f95a8460b6d4d9de/6693ed271489448f1cc1638b263727f2.webp)
16、hexbin图
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)
![](https://filescdn.proginn.com/b6a447fbcd006da76cb4cca9a952d485/36859c67879635b5d690eb89777f4f71.webp)
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")
![](https://filescdn.proginn.com/0623b3d70cc55810a2f6723bb23dcfc2/8b5426bf1f596be0e638b885619f52c4.webp)
18、核密度图
ser = pd.Series(np.random.randn(1000))
ser.plot.kde()
![](https://filescdn.proginn.com/d5a61cf13e9465e05ce09b617fbecb08/0be0cd158db16ba1650f2b73cde35979.webp)
19、parallel_coordinates图
from pandas.plotting import parallel_coordinates
data = pd.read_csv("iris.data.txt")
plt.figure()
parallel_coordinates(data, "Name")
![](https://filescdn.proginn.com/c35e73cc9f9c2d464bea20111fcb696b/b42467c8392ecd845e06c3812bf0bb3e.webp)
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)
![](https://filescdn.proginn.com/345714909c9227e772676b5e61addd95/09a9ee80eada71ef14e02cc2cfb5ad7f.webp)
21、radviz图
from pandas.plotting import radviz
data = pd.read_csv("iris.data.txt")
plt.figure()
radviz(data, "Name")
![](https://filescdn.proginn.com/0abb3cfad1116ecb0b49c428ce741591/aec6b277377e5f9c0cf2068ff0c78243.webp)
22、bootstrap_plot图
from pandas.plotting import bootstrap_plot
data = pd.Series(np.random.rand(1000))
bootstrap_plot(data, size=50, samples=500, color="grey")
![](https://filescdn.proginn.com/82ee3155e7697e1f75abc6bbfaac0ecb/a4676856acac0a3805eea826d5a157b8.webp)
23、子图(subplot)
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df.plot(subplots=True, figsize=(6, 6))
![](https://filescdn.proginn.com/614bbd2699632799dca5e57c8b16dcf0/c371c26aaac272b826cdd9e9ea7c5921.webp)
24、子图任意排列
df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False)
![](https://filescdn.proginn.com/bca17d86dc918fe0546b241022e8b9f1/5651b8ca3aeebbdbc7dca8491393b0a1.webp)
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)
![](https://filescdn.proginn.com/138e76b794b27fc9caef2396e83a1f67/14b8badc94ce41ddf31a8b09e78624b9.webp)
25、图中绘制数据表格
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);
![](https://filescdn.proginn.com/09ffd6bd848f52c4fbe6965540e4e047/97115ab0331aff97e78a3e7512f55741.webp)
27、更多pandas可视化精进资料
https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html#cookbook-plotting
-END-
推荐阅读:
一网打尽Pandas中的各种索引 iloc,loc,ix,iat,at,直接索引
↓扫描关注本号↓
评论