最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python繪圖中的?四個(gè)繪圖技巧

 更新時(shí)間:2021年12月14日 14:17:13   作者:春江暮客  
在可視化數(shù)據(jù)時(shí),通常需要在單個(gè)圖形中繪制多個(gè)圖形。?例如,如果您想從不同的角度可視化相同的變量如:數(shù)字變量的并排直方圖和箱線(xiàn)圖,則多個(gè)圖形很有用。?在這篇文章中,我分享了繪制多個(gè)圖形的?4?個(gè)簡(jiǎn)單但實(shí)用的技巧,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

數(shù)據(jù)集:

讓我們導(dǎo)入包并更新圖表的默認(rèn)設(shè)置,為圖表添加一點(diǎn)個(gè)人風(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()

繪制多個(gè)子圖的一種簡(jiǎn)單方法是使用 plt.subplots() 。

這是繪制 2 個(gè)并排子圖的示例語(yǔ)法:

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

在這里,我們?cè)谝粋€(gè)圖中繪制了兩個(gè)子圖。 我們可以進(jìn)一步自定義每個(gè)子圖。

?例如,我們可以像這樣為每個(gè)子圖添加標(biāo)題:

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

另一種可視化多個(gè)圖形的方法是使用 plt.subplot(),末尾沒(méi)有 s

?語(yǔ)法與之前略有不同:

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


當(dāng)我們想為多個(gè)圖繪制相同類(lèi)型的圖形并在單個(gè)圖中查看所有圖形,該方法特別有用:

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)


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


通過(guò)下面的比較,我們能更好的理解它們的相似處與不同處熟悉這兩種方法很有用,因?yàn)樗鼈兛梢栽诓煌闆r下派上用場(chǎng)。

技巧3: plt.tight_layout()

在繪制多個(gè)圖形時(shí),經(jīng)常會(huì)看到一些子圖的標(biāo)簽在它們的相鄰子圖上重疊,

如下所示:

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)


頂部?jī)蓚€(gè)圖表的 x 軸上的變量名稱(chēng)被剪掉,右側(cè)圖的 y 軸標(biāo)簽與左側(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()


專(zhuān)業(yè) 看起來(lái)更好了。

技巧4: plt.suptitle()

真?zhèn)€圖形添加標(biāo)題:

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ù)自己的喜好自定義各個(gè)圖。 例如,您仍然可以為每個(gè)子圖添加標(biāo)題。

到此這篇關(guān)于python繪圖 四個(gè)繪圖技巧的文章就介紹到這了,更多相關(guān)python 繪圖技巧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

广水市| 正镶白旗| 西青区| 兴义市| 卓资县| 江源县| 达州市| 巢湖市| 曲阜市| 惠水县| 灵寿县| 肥东县| 扎兰屯市| 桃江县| 诸暨市| 平泉县| 绥阳县| 信阳市| 江源县| 那坡县| 徐闻县| 呼图壁县| 勃利县| 达孜县| 淮阳县| 海阳市| 旬阳县| 贞丰县| 桂东县| 景泰县| 张家口市| 祥云县| 全州县| 吉安县| 无棣县| 双柏县| 剑阁县| 壶关县| 阿勒泰市| 沁水县| 三亚市|