TVP

# Python可视化工具介绍——Matplotlib（下）

1.Pandas Plot

Pandas整合了Matplotlib的画图功能，可以直接在Series,Dataframe上用plot()方法进行绘图，具体请参考：

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

quartiles = np.percentile(births['births'], [25, 50, 75])

mu, sig = quartiles[1], 0.74 * (quartiles[2] - quartiles[0])

births = births.query('(births > @mu - 5 * @sig) & (births

births['day'] = births['day'].astype(int)

births.index = pd.to_datetime(10000 * births.year +100 * births.month +births.day, format='%Y%m%d')

births_by_date = births.pivot_table('births',

[births.index.month, births.index.day])

births_by_date.index = [pd.datetime(2012, month, day)

for (month, day) in births_by_date.index]

fig, ax = plt.subplots(figsize=(12, 4))

births_by_date.plot(ax=ax);

## http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html

fig, ax = plt.subplots(2,1,figsize=(8, 10))

births.groupby('year').births.sum().plot(kind='pie',x='year',y='births',ax=ax[0])

births.groupby('month').births.sum().plot(kind='pie',x='year',y='births',ax=ax[1])

births.plot(subplots=True)

2.Seaborn

##密度曲线

for col in 'xy':

sns.distplot(data['x'])

sns.distplot(data['y']);##直方图与密度曲线同时显示

sns.pairplot(iris, hue='species', size=2.5); ##hue最终显示的标签颜色

tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill']

grid = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True)

grid.map(plt.hist, "tip_pct", bins=np.linspace(0, 40, 15));

## 显示x/y的直方图，右上角为相关系数

with sns.axes_style('white'):

sns.jointplot("total_bill", "tip", data=tips, kind='hex')

DIG

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180731G1LBEF00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2023-03-31

2023-03-31

2023-03-31

2023-03-31

2023-03-31

2023-03-31

2023-03-31

2023-03-31

2023-03-31

10元无门槛代金券