python繪圖中的?四個繪圖技巧
數(shù)據(jù)集:
讓我們導(dǎo)入包并更新圖表的默認設(shè)置,為圖表添加一點個人風(fēng)格。 我們將在提示上使用 Seaborn 的內(nèi)置數(shù)據(jù)集:
import seaborn as sns # v0.11.2
import matplotlib.pyplot as plt # v3.4.2
sns.set(style='darkgrid', context='talk', palette='rainbow')df = sns.load\_dataset('tips')
df.head()

技巧1: plt.subplots()
繪制多個子圖的一種簡單方法是使用 plt.subplots() 。
這是繪制 2 個并排子圖的示例語法:
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,4)) sns.histplot(data=df, x='tip', ax=ax[0]) sns.boxplot(data=df, x='tip', ax=ax[1]);

在這里,我們在一個圖中繪制了兩個子圖。 我們可以進一步自定義每個子圖。
?例如,我們可以像這樣為每個子圖添加標題:
fig, ax = plt.subplots(1, 2, figsize=(10,4))
sns.histplot(data=df, x='tip', ax=ax[0])
ax[0].set\_title("Histogram")
sns.boxplot(data=df, x='tip', ax=ax[1])
ax[1].set\_title("Boxplot");

在循環(huán)中將所有數(shù)值變量用同一組圖表示:
numerical = df.select\_dtypes('number').columnsfor col in numerical:
fig, ax = plt.subplots(1, 2, figsize=(10,4))
sns.histplot(data=df, x=col, ax=ax[0])
sns.boxplot(data=df, x=col, ax=ax[1]);
技巧2: plt.subplot()
另一種可視化多個圖形的方法是使用 plt.subplot(),末尾沒有 s
?語法與之前略有不同:
plt.figure(figsize=(10,4)) ax1 = plt.subplot(1,2,1) sns.histplot(data=df, x='tip', ax=ax1) ax2 = plt.subplot(1,2,2) sns.boxplot(data=df, x='tip', ax=ax2);

當我們想為多個圖繪制相同類型的圖形并在單個圖中查看所有圖形,該方法特別有用:
plt.figure(figsize=(14,4)) for i, col in enumerate(numerical): ax = plt.subplot(1, len(numerical), i+1) sns.boxplot(data=df, x=col, ax=ax)

我們同樣能定制子圖形。例如加個title
plt.figure(figsize=(14,4))
for i, col in enumerate(numerical):
ax = plt.subplot(1, len(numerical), i+1)
sns.boxplot(data=df, x=col, ax=ax)
ax.set\_title(f"Boxplot of {col}")

通過下面的比較,我們能更好的理解它們的相似處與不同處熟悉這兩種方法很有用,因為它們可以在不同情況下派上用場。
技巧3: plt.tight_layout()
在繪制多個圖形時,經(jīng)常會看到一些子圖的標簽在它們的相鄰子圖上重疊,
如下所示:
categorical = df.select\_dtypes('category').columnsplt.figure(figsize=(8, 8))
for i, col in enumerate(categorical):
ax = plt.subplot(2, 2, i+1)
sns.countplot(data=df, x=col, ax=ax)

頂部兩個圖表的 x 軸上的變量名稱被剪掉,右側(cè)圖的 y 軸標簽與左側(cè)子圖重疊.使用plt.tight_layout很方便
plt.figure(figsize=(8, 8)) for i, col in enumerate(categorical): ax = plt.subplot(2, 2, i+1) sns.countplot(data=df, x=col, ax=ax) plt.tight\_layout()

專業(yè) 看起來更好了。
技巧4: plt.suptitle()
真?zhèn)€圖形添加標題:
plt.figure(figsize=(8, 8))
for i, col in enumerate(categorical):
ax = plt.subplot(2, 2, i+1)
sns.countplot(data=df, x=col, ax=ax)
plt.suptitle('Category counts for all categorical variables')
plt.tight\_layout()
此外,您可以根據(jù)自己的喜好自定義各個圖。 例如,您仍然可以為每個子圖添加標題。
到此這篇關(guān)于python繪圖 四個繪圖技巧的文章就介紹到這了,更多相關(guān)python 繪圖技巧內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python list格式數(shù)據(jù)excel導(dǎo)出方法
今天小編就為大家分享一篇python list格式數(shù)據(jù)excel導(dǎo)出方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Pycharm配置導(dǎo)入torch報錯Traceback的問題及解決
這篇文章主要介紹了Pycharm配置導(dǎo)入torch報錯Traceback的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Python 基于win32com客戶端實現(xiàn)Excel操作的詳細過程
這篇文章主要介紹了Python 基于win32com客戶端實現(xiàn)Excel操作的詳細過程,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)詳解
在學(xué)習(xí)python過程中常遇到一種情況,要讀取.csv文件的數(shù)據(jù),然后取出其中某個字段,下面這篇文章主要給大家介紹了關(guān)于Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-08-08

