Pandas绘图df.plot(kind=‘...‘)

line’ : line plot (default)#折线图

‘bar’ : vertical bar plot#条形图。stacked为True时为堆叠的柱状图

‘barh’ : horizontal bar plot#横向条形图

‘hist’ : histogram#直方图(数值频率分布)

‘box’ : boxplot#箱型图

‘kde’ : Kernel Density Estimation plot#密度图,主要对柱状图添加Kernel 概率密度线

‘density’ : same as ‘kde’

‘area’ : area plot#与x轴所围区域图(面积图)。Stacked=True时,每列必须全部为正或负值,
stacked=False时,对数据没有要求

‘pie’ : pie plot#饼图。数值必须为正值,需指定Y轴或者subplots=True

‘scatter’ : scatter plot#散点图。需指定X轴Y轴

‘hexbin’ : hexbin plot#蜂巢图。需指定X轴Y轴
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# 创建一个小DataFrame
df = pd.DataFrame(index=['Atiya', 'Abbas', 'Cornelia', 'Stephanie', 'Monte'], 
                           data={'Apples':[20, 10, 40, 20, 50],
                                 'Oranges':[35, 40, 25, 19, 33]})
df
  Apples Oranges
Atiya 20 35
Abbas 10 40
Cornelia 40 25
Stephanie 20 19
Monte 50 33
# 画柱状图,使用行索引做x轴,列的值做高度,使用plot方法,参数kind设为bar;#黑白的像素是一个 0~1 之间的数字
# color = ['R', 'g'] #红绿 
color = ['.2', '.7']
#二值图像(Binary Image),按名字来理解只有两个值,0和1,0代表黑,1代表白,
#或者说0表示背景,而1表示前景。其保存也相对简单,每个像素只需要1Bit就可以完整存储信息。
#彩色图像,每个像素通常是由红(R)、绿(G)、蓝(B)三个分量来表示的,分量介于(0,255)
df.plot(kind='bar', color=color, figsize=(16,4))

# KDE图忽略行索引,使用每列的值作为x轴,并计算y值得概率密度
df.plot(kind='kde', color=color, figsize=(16 , 4)) 
#调用plot时加上kind='kde'即可生成一张密度图(标准混合正态分布KDE)
#表示figure 的大小为长、宽(单位为inch)

# 画三张双变量子图。散点图是唯一需要指定x和y轴的列,如果想在散点图中使用行索引,可以使用方法reset_index。

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16,4))
fig.suptitle('Two Variable Plots', size=20, y=1.02)
df.plot(kind='line', color=color, ax=ax1, title='Line plot') 
#绘制散点图时可能会出现“ c”参数包含2个元素,与大小为5的“ x”和“ y”不一致的情况所以c需要重新设置
#ValueError: 'c' argument has 2 elements, which is inconsistent with 'x' and 'y' with size 5.
colors=['.2', '.7','.8','.91','.100']
df.plot(x='Apples', y='Oranges', kind='scatter', color=
        colors, ax=ax2, title='Scatterplot')

df.plot(kind='bar', color=color, ax=ax3, title='Bar plot') #条形图

# 将单变量图也画在同一张图中
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16,4))
# plot可以绘出精美的图形,但是如果想要在一张图中展示多个子图,plot就很难办了。
# matplotlib提供了subplot来解决这一问题。(其实很像matlab中的绘图方法)
# plt.subplot(nrows=1, ncols=2, index=1,) 
#figsize 调整大小
fig.suptitle('One Variable Plots', size=20, y=1.02)#标题
df.plot(kind='kde', color=color, ax=ax1, title='KDE plot') 
#kind='kde'即可生成一张密度图(标准混合正态分布KDE)核密度曲线(kde)
df.plot(kind='box', ax=ax2, title='Boxplot') #箱线图
df.plot(kind='hist', color=color, ax=ax3, title='Histogram') #直方图(hist)

更多:

# matplotlib允许手动指定x和y的值
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16,4))
df.sort_values('Apples').plot(x='Apples', y='Oranges', kind='line', ax=ax1)
df.plot(x='Apples', y='Oranges', kind='bar', ax=ax2)
df.plot(x='Apples', kind='kde', ax=ax3)
#df.plot(kind='kde', color=color, ax=ax1, title='KDE plot') #标准混合正态分布KDE)核密度曲线
#df.plot(kind='box', ax=ax2, title='Boxplot') #箱线图
#df.plot(kind='hist', color=color, ax=ax3, title='Histogram') #直方图(hist)

猜你喜欢

转载自blog.csdn.net/weixin_48135624/article/details/114261631