pandas绘图介绍

http://pandas.pydata.org/pandas-docs/stable/visualization.html#

http://www.360doc.com/content/17/0809/00/14207430_677680396.shtml

首先引入 matplotlib 标准库:  import matplotlib.pyplot as plt

Note: All calls to np.random are seeded with 123456

把下方的代码,放到一个python文件中,运行即可看到效果图。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Note: All calls to np.random are seeded with 123456.
np.random.seed(123456)
# 一个简单的随机漫步图
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2018', periods=1000))
ts.cumsum().plot()
# DataFrame 每列的一个简单的随机漫步图
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df.cumsum().plot()
# 用关键字 x , y
df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
df3['A'] = pd.Series(list(range(len(df))))
df3.plot(x='A', y='B')
# pandas 中其他一些种类的图形
# • ‘bar’ or ‘barh’ for bar plots
# • ‘hist’ for histogram
# • ‘box’ for boxplot
# • ‘kde’ or ‘density’ for density plots
# • ‘area’ for area plots
# • ‘scatter’ for scatter plots
# • ‘hexbin’ for hexagonal bin plots
# • ‘pie’ for pie plots
# 有两种方式画图
# 1)kind 关键字
plt.figure()
df.iloc[5].plot(kind='bar')
# 2) 调用 plot 的方法
plt.figure()
df.iloc[5].plot.bar()
plt.axhline(0, color='b')
# 创建一个多条柱形图
df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df2.plot.bar()
# 创建一个堆叠式的柱形图
df2.plot.bar(stacked=True)
plt.legend(loc='upper right')
# 创建一个水平的柱状图
df2.plot.barh(stacked=True)
# 直方图 Histograms
# Histograms can be drawn by using the DataFrame.plot.hist()
# and Series.plot.hist() methods
df4 = pd.DataFrame({'a': np.random.randn(1000) + 1,
                    'b': np.random.randn(1000),
                    'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df4.plot.hist(alpha=0.5)
# 直方图也可以堆叠
df4.plot.hist(stacked=True, bins=20)
# You can pass other keywords supported by matplotlib hist.
# For example, horizontal and cumulative histograms can
# be drawn by orientation='horizontal' and cumulative=True.
plt.figure()
df4['a'].plot.hist(orientation='horizontal', cumulative=True)
# 箱形图(Box-plot)又称为盒须图、盒式图或箱线图,
# 是一种用作显示一组数据分散情况资料的统计图
df5 = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
# print(df5)
df5.plot.box()
# 画区域图 Area Plot
df6 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df6.plot.area()  # 默认堆叠
df6.plot.area(stacked=False)
# 散点图 Scatter Plot
# Scatter plot requires numeric columns for the x and y axes.
# These can be specified by the x and y keywords.
df7 = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df7.plot.scatter(x='a', y='b')  # 画散点图
# 在一个图中画多个列的分类,可以分别指定不同的颜色和标签
ax = df7.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1')
df7.plot.scatter(x='c', y='d', color='DarkGreen', label='Group 2', ax=ax)
plt.legend(loc='upper right')
# The keyword c may be given as the name of a column
# to provide colors for each point
df7.plot.scatter(x='a', y='b', c='c', s=50)
# 还有一些其他的参数,有兴趣可以看一下
df7.plot.scatter(x='a', y='b', s=df7['c']*200);
#  饼状图 Pie plot
# You can create a pie plot with DataFrame.plot.pie() or Series.plot.pie().
# If your data includes any NaN, they will be automatically filled with 0.
# A ValueError will be raised if there are any negative values in your data.
plt.figure()
series = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
series.plot.pie(figsize=(6, 6))
series.plot.pie(labels=['AA', 'BB', 'CC', 'DD'],
                colors=['r', 'g', 'b', 'c'],
                autopct='%.2f', fontsize=20, figsize=(6, 6))
# If you pass values whose sum total is less than 1.0,
# matplotlib draws a semicircle
series = pd.Series([0.1] * 4, index=['a', 'b', 'c', 'd'], name='series2')
plt.figure()
series.plot.pie(figsize=(6, 6))
# pie plot with DataFrame requires that you either specify a target column by the y argument or
# subplots=True. When y is specified, pie plot of selected column will be drawn.
# If subplots=True is specified, pie plots for each column are drawn as subplots.
# A legend will be drawn in each pie plots by default; specify
# legend=False to hide it.
df = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])
df.plot.pie(subplots=True, figsize=(8, 4))
plt.show()



猜你喜欢

转载自blog.csdn.net/qq_42413820/article/details/80759810