溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python怎么把幾個(gè)圖表一起在同一張圖上顯示

發(fā)布時(shí)間:2022-03-22 15:17:34 來源:億速云 閱讀:4702 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“python怎么把幾個(gè)圖表一起在同一張圖上顯示”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“python怎么把幾個(gè)圖表一起在同一張圖上顯示”吧!

1:怎么把幾個(gè)圖表一起在同一張圖上顯示?

關(guān)鍵代碼
import matplotlib.pyplot as plt

# 設(shè)置figure_size尺寸
plt.rcParams['figure.figsize'] = (8.0, 6.0)

fig = plt.figure()

# 設(shè)定圖表顏色
fig.set(alpha=0.2)

# 第一張小圖
plt.subplot2grid((2,3),(0,0))
data_train['Survived'].value_counts().plot(kind='bar')
plt.ylabel(u"人數(shù)")
plt.title(u"船員獲救情況 (1為獲救)")

# 第二張小圖
plt.subplot2grid((2,3),(0,1))
data_train['Pclass'].value_counts().plot(kind="bar")
plt.ylabel(u"人數(shù)")
plt.title(u"乘客等級(jí)分布")

# 第三張小圖
plt.subplot2grid((2,3),(0,2))
plt.scatter(data_train['Survived'], data_train['Age'])
plt.ylabel(u"年齡")
plt.grid(b=True, which='major', axis='y')
plt.title(u"按年齡看獲救分布 (1為獲救)")

# 第四張小圖,分布圖
plt.subplot2grid((2,3),(1,0), colspan=2)
data_train.Age[data_train.Pclass == 1].plot(kind='kde')
data_train.Age[data_train.Pclass == 2].plot(kind='kde')
data_train.Age[data_train.Pclass == 3].plot(kind='kde')
plt.xlabel(u"年齡")
plt.ylabel(u"密度")
plt.title(u"各等級(jí)的乘客年齡分布")
plt.legend((u'頭等艙', u'2等艙',u'3等艙'),loc='best')

# 第五張小圖
plt.subplot2grid((2,3),(1,2))
data_train.Embarked.value_counts().plot(kind='bar')
plt.title(u"各登船口岸上船人數(shù)")
plt.ylabel(u"人數(shù)")
plt.show()

python怎么把幾個(gè)圖表一起在同一張圖上顯示

我們從上面的可視化操作結(jié)果可以看出,其實(shí)可以看出一些規(guī)律,比如說生還的幾率比死亡的要大,然后獲救的人在年齡上區(qū)別不大,然后就是有錢人(坐頭等艙的)的年齡會(huì)偏大等。

2:如何使用sklearn的多項(xiàng)式來衍生更多的變量?

關(guān)于這種衍生變量的方式,理論其實(shí)大家應(yīng)該很早也都聽說過了,但是如何在Python里實(shí)現(xiàn),也就是今天在這里分享給大家,其實(shí)也很簡單,就是調(diào)用sklearnPolynomialFeatures方法,具體大家可以看看下面的demo。

這里使用一個(gè)人體加速度數(shù)據(jù)集,也就是記錄一個(gè)人在做不同動(dòng)作時(shí)候,在不同方向上的加速度,分別有3個(gè)方向,命名為x、y、z。

關(guān)鍵代碼
# 擴(kuò)展數(shù)值特征
from sklearn.preprocessing import PolynomialFeatures

x = df[['x','y','z']]
y = df['activity']

poly = PolynomialFeatures(degree=2, include_bias=False, interaction_only=False)

x_poly = poly.fit_transform(x)
pd.DataFrame(x_poly, columns=poly.get_feature_names()).head()

python怎么把幾個(gè)圖表一起在同一張圖上顯示

就這樣子簡單的去調(diào)用,就可以生成了很多的新變量了。

3:如何把分布修正為類正態(tài)分布?

今天我們用的是一個(gè)新的數(shù)據(jù)集,也是在kaggle上的一個(gè)比賽,大家可以先去下載一下:

python怎么把幾個(gè)圖表一起在同一張圖上顯示

import pandas as pd
import numpy as np
# Plots
import seaborn as sns
import matplotlib.pyplot as plt

# 讀取數(shù)據(jù)集
train = pd.read_csv('./data/house-prices-advanced-regression-techniques/train.csv')
train.head()

python怎么把幾個(gè)圖表一起在同一張圖上顯示

首先這個(gè)是一個(gè)價(jià)格預(yù)測的題目,在開始前我們需要看看分布情況,可以調(diào)用以下的方法來進(jìn)行繪制:

sns.set_style("white")
sns.set_color_codes(palette='deep')
f, ax = plt.subplots(figsize=(8, 7))
#Check the new distribution
sns.distplot(train['SalePrice'], color="b");
ax.xaxis.grid(False)
ax.set(ylabel="Frequency")
ax.set(xlabel="SalePrice")
ax.set(title="SalePrice distribution")
sns.despine(trim=True, left=True)
plt.show()

python怎么把幾個(gè)圖表一起在同一張圖上顯示

我們從結(jié)果可以看出,銷售價(jià)格是右偏,而大多數(shù)機(jī)器學(xué)習(xí)模型都不能很好地處理非正態(tài)分布數(shù)據(jù),所以我們可以應(yīng)用log(1+x)轉(zhuǎn)換來進(jìn)行修正。那么具體我們可以怎么用Python代碼實(shí)現(xiàn)呢?

# log(1+x) 轉(zhuǎn)換
train["SalePrice_log"] = np.log1p(train["SalePrice"])

sns.set_style("white")
sns.set_color_codes(palette='deep')
f, ax = plt.subplots(figsize=(8, 7))

sns.distplot(train['SalePrice_log'] , fit=norm, color="b");

# 得到正態(tài)分布的參數(shù)
(mu, sigma) = norm.fit(train['SalePrice_log'])

plt.legend(['Normal dist. ($\mu=$ {:.2f} and $\sigma=$ {:.2f} )'.format(mu, sigma)],
           loc='best')
ax.xaxis.grid(False)
ax.set(ylabel="Frequency")
ax.set(xlabel="SalePrice")
ax.set(title="SalePrice distribution")
sns.despine(trim=True, left=True)

plt.show()

python怎么把幾個(gè)圖表一起在同一張圖上顯示

到此,相信大家對(duì)“python怎么把幾個(gè)圖表一起在同一張圖上顯示”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI