基本圖形
柱狀圖
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