日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】pandas一行代码绘制26种美图

發布時間:2025/3/12 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】pandas一行代码绘制26种美图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文目錄

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")#默認繪制折線圖

2、多組折線圖

np.random.seed(0) df?=?pd.DataFrame(np.random.randn(1000,?4),?index=ts.index,?columns=list("ABCD")) df?=?df.cumsum() df.plot()#默認繪制折線圖

3、單組條形圖

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

4、多組條形圖

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

5、堆積條形圖

df2.plot.bar(stacked=True)

6、水平堆積條形圖

df2.plot.barh(stacked=True)

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)

8、分面直方圖

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

9、箱圖

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

10、面積圖

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

11、堆積面積圖

df.plot.area(stacked=False)

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)

13、單組餅圖

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

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))

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()

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)

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、核密度圖

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圖

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

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")

23、子圖(subplot)

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

24、子圖任意排列

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、圖中繪制數據表格

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、更多pandas可視化精進資料

https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html#cookbook-plotting

-END-

往期精彩回顧適合初學者入門人工智能的路線及資料下載機器學習及深度學習筆記等資料打印機器學習在線手冊深度學習筆記專輯《統計學習方法》的代碼復現專輯 AI基礎下載機器學習的數學基礎專輯黃海廣老師《機器學習課程》課件合集 本站qq群851320808,加入微信群請掃碼:

總結

以上是生活随笔為你收集整理的【Python】pandas一行代码绘制26种美图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。