python_matplotlib各种图

本文以委托蝙蝠侠、蜘蛛侠叮当猫等人进行维权,未经许可,禁止转载!!!!

一、画线

import matplotlib.pyplot as plt

x = [1,2,3]
y = [1,20,3]
x2 = [2,3,4]
y2 = [2,14,8]
plt.plot(x,y,label='First one')
plt.plot(x2,y2,label='Second one')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting\nChick it out')
plt.legend()
plt.show

二、

画柱状图(bar)

import matplotlib.pyplot as plt
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting\nChick it out')
x = [2,4,6,8,10]
y = [6,7,8,2,4]
x2 = [2,4,6,8,10]
y2 = [7,8,2,4,2]
plt.bar(x,y,label='bar1',color='r')
plt.bar(x2,y2,label='bar2',color='c')
plt.legend()
plt.show()

画柱状图(hist)---将数据根据bins进行分类,使其更加有实用价值

import matplotlib.pyplot as plt
population_age = [22,55,62,45,21,22,34,42,42,9,99,102,110,121,122,130,112]

#ids = [x for x in range(len(population_age))]

#plt.bar(ids,population_age)
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]

plt.hist(population_age,bins,histtype='bar',rwidth = 0.5)
##横坐标表示范围
plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')

plt.legend()
plt.show()


画多个柱状图

http://baijiahao.baidu.com/s?id=1591538617221352925&wfr=spider&for=pc

三、画散点图

import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9]
y = [1,2,3,4,5,6,7,8,9]
plt.scatter(x,y,label='skit',color='k',marker = '*',s = 100)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')

四、画分层图---between a num and a num

import matplotlib.pyplot as plt

days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]

plt.stackplot(days,sleeping,eating,working,playing,colors = ['m','c','r','b'])

plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')
plt.legend()
plt.show()


五、画饼图

import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]

cos = ['b','r','y','g']
slices = [7,2,2,13]
activaties = ['sleeping','eating','working','playing']

plt.pie(slices,
        labels =activaties,
        colors = cos ,
        startangle = 90,   #从90度处开始循环
        shadow = True,    #有阴影,显得更加立体
        explode = (0,0,0,0.1),#把其中一块拉出来
        autopct='%1.1f%%')#显示比例

plt.xlabel('x')
plt.ylabel('y')
plt.title('ok')
plt.legend()
plt.show()

六、画多个子图


#!/usr/bin/env python
#!encoding=utf-8

import matplotlib
import matplotlib.pyplot as plt

if __name__ == '__main__':
    for i,color in enumerate("rgby"):
        plt.subplot(221+i, axisbg=color)

    plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

figure_1

3.2 不规则划分


但是有时候我们的划分并不是规则的, 比如如下的形式

figure_2

这种应该怎么划分呢?

将整个表按照 2*2 划分 
前两个简单, 分别是 (2, 2, 1) 和 (2, 2, 2)

但是第三个图呢, 他占用了 (2, 2, 3) 和 (2, 2, 4)

显示需要对其重新划分, 按照 2 * 1 划分

前两个图占用了 (2, 1, 1) 的位置

因此第三个图占用了 (2, 1, 2) 的位置

figure_3

代码如下, 参照matplotlib绘制多个子图——subplot

#!/usr/bin/env python
#!encoding=utf-8

import matplotlib.pyplot as plt
import numpy as np



def f(t):
    return np.exp(-t) * np.cos(2 * np.pi * t)

if __name__ == '__main__' :
    t1 = np.arange(0, 5, 0.1)
    t2 = np.arange(0, 5, 0.02)

    plt.figure(12)
    plt.subplot(221)
    plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')

    plt.subplot(222)
    plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')

    plt.subplot(212)
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

    plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26




  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值