Pandas 數(shù)據(jù)可視化

基本圖形

柱狀圖

reviews['points'].value_counts().sort_index().plot.bar()

散點圖

reviews[reviews['price'] < 100].sample(100).plot.scatter(x='price', y='points')

image

蜂窩圖

reviews[reviews['price'] < 100].plot.hexbin(x='price', y='points', gridsize=15)

image

大量重復(fù)的點可以用這種圖表示

柱狀圖-疊加模式

image
wine_counts.plot.bar(stacked=True)

image

面積模式

wine_counts.plot.area()

折線模式

wine_counts.plot.line()

美化

設(shè)置圖的大小真屯,字體大小讨跟,顏色晾匠,標(biāo)題

reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12, 6),
    color='mediumvioletred',
    fontsize=16,
    title='Rankings Given by Wine Magazine',
)

借助Matplotlib

import matplotlib.pyplot as plt

ax = reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12, 6),
    color='mediumvioletred',
    fontsize=16
)
ax.set_title("Rankings Given by Wine Magazine", fontsize=20)

image

借助Seaborn-去除邊框

import matplotlib.pyplot as plt
import seaborn as sns

ax = reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12, 6),
    color='mediumvioletred',
    fontsize=16
)
ax.set_title("Rankings Given by Wine Magazine", fontsize=20)
sns.despine(bottom=True, left=True)

image

多圖表

matplotlib

fig, axarr = plt.subplots(2, 2, figsize=(12, 8))

reviews['points'].value_counts().sort_index().plot.bar(
    ax=axarr[0][0]
)

reviews['province'].value_counts().head(20).plot.bar(
    ax=axarr[1][1]

image

Seaborn

df = footballers[footballers['Position'].isin(['ST', 'GK'])]
g = sns.FacetGrid(df, col="Position", col_wrap=2)
g.map(sns.kdeplot, "Overall")

image
df = footballers[footballers['Position'].isin(['ST', 'GK'])]
df = df[df['Club'].isin(['Real Madrid CF', 'FC Barcelona', 'Atlético Madrid'])]

g = sns.FacetGrid(df, row="Position", col="Club")
g.map(sns.violinplot, "Overall")

image
df = footballers[footballers['Position'].isin(['ST', 'GK'])]
df = df[df['Club'].isin(['Real Madrid CF', 'FC Barcelona', 'Atlético Madrid'])]

g = sns.FacetGrid(df, row="Position", col="Club", 
                  row_order=['GK', 'ST'],
                  col_order=['Atlético Madrid', 'FC Barcelona', 'Real Madrid CF'])
g.map(sns.violinplot, "Overall")

控制顯示順序

pairplot-多變量的相互關(guān)系

sns.pairplot(footballers[['Overall', 'Potential', 'Value']])

image

顏色澜共,圖標(biāo)參數(shù)

sns.lmplot(
  x='Value', y='Overall', 
  markers=['o', 'x', '*'], 
  hue='Position', 
  data=footballers.loc[footballers['Position'].isin(
    ['ST', 'RW', 'LW'])],
  fit_reg=False
)

image

分組

f = (footballers
         .loc[footballers['Position'].isin(['ST', 'GK'])]
         .loc[:, ['Value', 'Overall', 'Aggression', 'Position']]
    )
f = f[f["Overall"] >= 80]
f = f[f["Overall"] < 85]
f['Aggression'] = f['Aggression'].astype(float)

sns.boxplot(x="Overall", y="Aggression", hue='Position', data=f)

image

總結(jié)圖

熱力圖

f = (
    footballers.loc[:, ['Acceleration', 'Aggression', 'Agility', 'Balance', 'Ball control']]
        .applymap(lambda v: int(v) if str.isdecimal(v) else np.nan)
        .dropna()
).corr()

sns.heatmap(f, annot=True)

image

平行線圖

from pandas.plotting import parallel_coordinates

f = (
    footballers.iloc[:, 12:17]
        .loc[footballers['Position'].isin(['ST', 'GK'])]
        .applymap(lambda v: int(v) if str.isdecimal(v) else np.nan)
        .dropna()
)
f['Position'] = footballers['Position']
f = f.sample(200)

parallel_coordinates(f, 'Position')

image

Seanborn使用

基本圖形

柱狀圖-值統(tǒng)計

countplot == value_count

sns.countplot(reviews['points'])

image

折線圖-密度圖

sns.kdeplot(reviews.query('price < 200').price)

image

二維密度圖--類似蜂窩圖作用

樣本多京革,重復(fù)點多的時候用

sns.kdeplot(reviews[reviews['price'] < 200].loc[:, ['price', 'points']].dropna().sample(5000))

image

直方圖

類似pandas.hist

sns.distplot(reviews['points'], bins=10, kde=False)

image

散點圖和直方圖復(fù)合

sns.jointplot(x='price', y='points', data=reviews[reviews['price'] < 100])

image

蜂窩圖和直方圖復(fù)合

sns.jointplot(x='price', y='points', data=reviews[reviews['price'] < 100], kind='hex',gridsize=20)

image

箱線圖

df = reviews[reviews.variety.isin(reviews.variety.value_counts().head(5).index)]
sns.boxplot(
    x='variety',
    y='points',
    data=df
)

image

小提琴圖

sns.violinplot(
    x='variety',
    y='points',
    data=reviews[reviews.variety.isin(reviews.variety.value_counts()[:5].index)]
)

image

網(wǎng)絡(luò)動態(tài)圖表-plotly

from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)

散點圖

import plotly.graph_objs as go

iplot([go.Scatter(x=reviews.head(1000)['points'], y=reviews.head(1000)['price'], mode='markers')])

image

熱力圖

iplot([go.Histogram2dContour(x=reviews.head(500)['points'], 
                             y=reviews.head(500)['price'], 
                             contours=go.Contours(coloring='heatmap')),
       go.Scatter(x=reviews.head(1000)['points'], y=reviews.head(1000)['price'], mode='markers')])

image

圖形語法的可視化庫plotnine

from plotnine import *

top_wines = reviews[reviews['variety'].isin(reviews['variety'].value_counts().head(5).index)]

df = top_wines.head(1000).dropna()

(ggplot(df)
 + aes('points', 'price')
 + geom_point())

#其他表達形式ggplot(df)
 + geom_point(aes('points', 'price'))
)

(ggplot(df, aes('points', 'price'))
 + geom_point

一層層添加圖形參數(shù)

image
df = top_wines.head(1000).dropna()

(
    ggplot(df)
        + aes('points', 'price')
        + geom_point()
        + stat_smooth()
)

image

添加顏色

df = top_wines.head(1000).dropna()

(
    ggplot(df)
        + geom_point()
        + aes(color='points')
        + aes('points', 'price')
        + stat_smooth()
)

一圖多表

df = top_wines.head(1000).dropna()

(ggplot(df)
     + aes('points', 'price')
     + aes(color='points')
     + geom_point()
     + stat_smooth()
     + facet_wrap('~variety')
)

image

柱狀圖

(ggplot(top_wines)
     + aes('points')
     + geom_bar()
)

image

二維熱力圖

(ggplot(top_wines)
     + aes('points', 'variety')
     + geom_bin2d(bins=20)
)

image

更多API文檔 API Reference.

處理時間序列

一般柱狀圖

shelter_outcomes['date_of_birth'].value_counts().sort_values().plot.line()

image

按年份重新取樣

shelter_outcomes['date_of_birth'].value_counts().resample('Y').sum().plot.line()

image
stocks['volume'].resample('Y').mean().plot.bar()

image

同期對比

如今年12月和去年12月比較

from pandas.plotting import lag_plot

lag_plot(stocks['volume'].tail(250))

image

自相關(guān)圖

from pandas.plotting import autocorrelation_plot

autocorrelation_plot(stocks['volume'])

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市棉钧,隨后出現(xiàn)的幾起案子宪卿,更是在濱河造成了極大的恐慌,老刑警劉巖西疤,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件代赁,死亡現(xiàn)場離奇詭異芭碍,居然都是意外死亡孽尽,警方通過查閱死者的電腦和手機杉女,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門熏挎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人烦磁,你說我怎么就攤上這事廉白『秕澹” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長聋溜。 經(jīng)常有香客問我撮躁,道長,這世上最難降的妖魔是什么杨帽? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任注盈,我火速辦了婚禮老客,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鳍鸵。我一直安慰自己朴则,他們只是感情好乌妒,可當(dāng)我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布撤蚊。 她就那樣靜靜地躺著,像睡著了一般槽唾。 火紅的嫁衣襯著肌膚如雪庞萍。 梳的紋絲不亂的頭發(fā)上忘闻,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天齐佳,我揣著相機與錄音,去河邊找鬼本鸣。 笑死荣德,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的曹傀。 我是一名探鬼主播饲宛,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼久锥!你這毒婦竟也來了瑟由?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤歹苦,失蹤者是張志新(化名)和其女友劉穎青伤,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體殴瘦,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡狠角,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蚪腋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丰歌。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖立帖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情悠砚,我是刑警寧澤晓勇,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站哩簿,受9級特大地震影響宵蕉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜节榜,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一羡玛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宗苍,春花似錦稼稿、人聲如沸薄榛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽敞恋。三九已至,卻和暖如春谋右,著一層夾襖步出監(jiān)牢的瞬間硬猫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工改执, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留啸蜜,地道東北人。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓辈挂,卻偏偏與公主長得像衬横,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子终蒂,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容