matplotlib绘制基本图表???

从上一篇博客到现在大概有两周不更新了,原是有一天发烧难受,所以那天就没更新,断一天不要紧,这一下子就懒了两周,每天总是给自己找各种借口,忙着刷知乎,看电影,逛淘宝,品热搜,真是玩的不亦乐乎,果然喜乐的东西人人都爱~

1.基本图表控制参数

首先引入matplotlib绘图所需模块

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

我们创建一维数组Series数组生成我们想要的数据

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))#date_range是pandas中的一个跟时间有关的参数,periods为时间间隔
ts = ts.cumsum()#求累加值

直接调用plot参数生成图表

ts.plot(kind='line',
       label = 'hehe',
       style = '--g.',
       color = 'red',
       alpha = 0.4,
       use_index = True,
       rot = 45,
       #grid = True,
       ylim = [-50,50],
       yticks = list(range(-50,50,10)),
       figsize = (8,4),
       title = 'test',
       #legend = True
       )#这是绘制图表的一些基本参数
plt.legend()#显示标注
plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x')  # 网格
#以下是它的基本参数
# Series.plot():series的index为横坐标,value为纵坐标
# kind → line,bar,barh...(折线图,柱状图,柱状图-横...)
# label → 图例标签,Dataframe格式以列名为label
# style → 风格字符串,这里包括了linestyle(-),marker(.),color(g)
# color → 颜色,有color指定时候,以color颜色为准
# alpha → 透明度,0-1
# use_index → 将索引用为刻度标签,默认为True
# rot → 旋转刻度标签,0-360
# grid → 显示网格,一般直接用plt.grid
# xlim,ylim → x,y轴界限
# xticks,yticks → x,y轴刻度值
# figsize → 图像大小
# title → 图名
# legend → 是否显示图例,一般直接用plt.legend()
# 也可以 → plt.plot()

输出结果:
在这里插入图片描述
再来看通过二维数组Dataframe绘制图表
创建数据

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()

绘制图表

df.plot(kind='line',
       style = '--.',
       alpha = 0.4,
       use_index = True,
       rot = 45,
       grid = True,
       figsize = (8,4),
       title = 'test',
       legend = True,
       subplots = False,#是否将各个列绘制到不同图表,默认False
       colormap = 'Greens')
       

输出结果:
在这里插入图片描述
以上是图表绘制基本控制参数,下面继续写一下柱状图与堆叠图绘制的方法及注意事项

2.绘制柱状图与堆叠图

创建4个子图,并设置图形大小

fig,axes = plt.subplots(4,1,figsize = (10,10))#创建4个子图

(1)单系列柱状图
生成Series一维数组数据

s = pd.Series(np.random.randint(0,10,16),index = list('abcdefghijklmnop')) 

绘制柱状图

# 单系列柱状图方法一:plt.plot(kind='bar/barh')
s.plot(kind='bar',color = 'k',grid = True,alpha = 0.5,ax = axes[0])  # ax参数 → 选择第几个子图

输出结果:
在这里插入图片描述
(2)多系列柱状图
生成Dataframe数据

df = pd.DataFrame(np.random.rand(10,3), columns=['a','b','c'])

绘制多系列柱状图

df.plot(kind='bar',ax = axes[1],grid = True,colormap='Reds_r')

输出结果:
在这里插入图片描述
(3)多系列堆叠图
多系列堆叠图是把多系列柱状图堆叠到一起,生成一个柱体,很容易观察到各部分占比情况
纵向堆叠图

df.plot(kind='bar',ax = axes[2],grid = True,colormap='Blues_r',stacked=True) 
# stacked → 堆叠

输出结果:

在这里插入图片描述
横向堆叠图

df.plot.barh(ax = axes[3],grid = True,stacked=True,colormap = 'BuGn_r')

输出结果:
在这里插入图片描述
(4)上下对比柱状图
如果要对比两组数据的结果,我们可以将它们绘制到一张图表,并上下分布

首先生成图表和数据

plt.figure(figsize=(10,4))
x = np.arange(10)#横坐标轴
y1 = np.random.rand(10)#数据1
y2 = -np.random.rand(10)#数据2

绘制上下对比柱状图

plt.bar(x,y1,width = 1,facecolor = 'yellowgreen',edgecolor = 'white',yerr = y1*0.1)
plt.bar(x,y2,width = 1,facecolor = 'lightskyblue',edgecolor = 'white',yerr = y2*0.1)
# x,y参数:x,y值
# width:宽度比例
# facecolor柱状图里填充的颜色、edgecolor是边框的颜色
# left-每个柱x轴左边界,bottom-每个柱y轴下边界 → bottom扩展即可化为甘特图 Gantt Chart
# align:决定整个bar图分布,默认left表示默认从左边界开始绘制,center会将图绘制在中间位置
# xerr/yerr :x/y方向error bar

输出结果:
在这里插入图片描述
(5)外嵌图表
我们在绘制图表的时候可以吧数据表嵌在图表下面,说实话我觉得看起来不是很美观,不建议这种方式
创建数据


data = [[ 66386, 174296,  75131, 577908,  32015],
        [ 58230, 381139,  78045,  99308, 160454],
        [ 89135,  80552, 152558, 497981, 603535],
        [ 78415,  81858, 150656, 193263,  69638],
        [139361, 331509, 343164, 781380,  52269]]
columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]
df = pd.DataFrame(data,columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail'),
                 index = ['%d year' % x for x in (100, 50, 20, 10, 5)])
print(df)

输出结果:
在这里插入图片描述
创建堆叠图

df.plot(kind='bar',grid = True,colormap='Blues_r',stacked=True,figsize=(8,3))

输出结果:
在这里插入图片描述

嵌入数据表

plt.table(cellText = data,
          cellLoc='center',
          cellColours = None,
          rowLabels = rows,
          rowColours = plt.cm.BuPu(np.linspace(0, 0.5,5))[::-1],  # BuPu可替换成其他colormap
          colLabels = columns,
          colColours = plt.cm.Reds(np.linspace(0, 0.5,5))[::-1], 
          rowLoc='right',
          loc='bottom')
plt.xticks([])# 不显示x轴标注
#参数注解:
# cellText:表格文本
# cellLoc:cell内文本对齐位置
# rowLabels:行标签
# colLabels:列标签
# rowLoc:行标签对齐位置
# loc:表格位置 → left,right,top,bottom

输出结果:
在这里插入图片描述
这篇博客暂且到此

关注欢喜,走向成功~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值