開心生活站

位置:首頁 > 學習教育 > 

python繪圖中的四個繪圖技巧

<link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b2fbfaddf.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b38b8bad702ecfe21037ca964.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>

技巧1: plt.subplots()

技巧2: plt.subplot()

技巧3: plt.tight_layout()

技巧4: plt.suptitle()

數據集:

讓我們導入包並更新圖表的默認設置,爲圖表添加一點個人風格。 我們將在提示上使用 Seaborn 的內置數據集:

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

python繪圖中的四個繪圖技巧

技巧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]);

python繪圖中的四個繪圖技巧 第2張

在這裏,我們在一個圖中繪製了兩個子圖。 我們可以進一步自定義每個子圖。

 例如,我們可以像這樣爲每個子圖添加標題:

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

python繪圖中的四個繪圖技巧 第3張

在循環中將所有數值變量用同一組圖表示:

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

python繪圖中的四個繪圖技巧 第4張

當我們想爲多個圖繪製相同類型的圖形並在單個圖中查看所有圖形,該方法特別有用:

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)

python繪圖中的四個繪圖技巧 第5張

我們同樣能定製子圖形。例如加個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}")

python繪圖中的四個繪圖技巧 第6張

通過下面的比較,我們能更好的理解它們的相似處與不同處熟悉這兩種方法很有用,因爲它們可以在不同情況下派上用場。

技巧3: plt.tight_layout()

在繪製多個圖形時,經常會看到一些子圖的標籤在它們的相鄰子圖上重疊,

如下所示:

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)

python繪圖中的四個繪圖技巧 第7張

頂部兩個圖表的 x 軸上的變量名稱被剪掉,右側圖的 y 軸標籤與左側子圖重疊.使用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()

python繪圖中的四個繪圖技巧 第8張

專業 看起來更好了。

技巧4: plt.suptitle()

真個圖形添加標題:

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

此外,您可以根據自己的喜好自定義各個圖。 例如,您仍然可以爲每個子圖添加標題。

到此這篇關於python繪圖 四個繪圖技巧的文章就介紹到這了,希望大家以後多多支持好二三四!

標籤:Python 繪圖 四個