我们接着上次的继续讲解,先讲一个概念,叫子图的概念。

我们先看一下这段代码

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(3,2,1)
ax2 = fig.add_subplot(3,2,2)
ax3 = fig.add_subplot(3,2,3)
ax4 = fig.add_subplot(3,2,6)
plt.show()

我们看到plt.figure()这个方法,我们设置一个整体的图。然后我们在往这个图中增加我们需要的子图

fig.add_subplot(3,2,1)意思是在figure中,添加一个3行2列的子图,其中,最后一个参数是子图的位置

我们在做一下变换,对比一下可以很清楚的看到,我们看看下面的代码

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(4,4,1)
ax2 = fig.add_subplot(4,4,2)
ax3 = fig.add_subplot(4,4,3)
ax4 = fig.add_subplot(4,4,4)
ax5 = fig.add_subplot(4,4,6)
ax6 = fig.add_subplot(4,4,7)
ax6 = fig.add_subplot(4,4,16)
plt.show()

可以得到如下的一个子图

import numpy as np
fig = plt.figure(figsize=(6,6))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(np.random.randint(1,5,5),np.arange(5))
ax2.plot(np.arange(10)*3,np.arange(10))
plt.show()

我们可以看到,我们设置figsize=(6,6)我们可以图的大小。设置两个图

我们在画图的时候,可以直接通过ax1.plot,ax2.plot进行随机数据配置

unrate['MONTH'] = unrate['DATE'].dt.month
unrate['MONTH'] = unrate['DATE'].dt.month
fig = plt.figure(figsize=(6,3)) plt.plot(unrate[0:12]['MONTH'],unrate[0:12]['VALUE'],c ='red')
plt.plot(unrate[12:24]['MONTH'],unrate[12:24]['VALUE'],c ='blue')
plt.show()

可以看到我们可以设置折线图的不同的颜色。

下面我们看看这个例子

fig = plt.figure(figsize=(10,6))
colors = ['red','blue','green','orange','black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
plt.plot(subset['MONTH'],subset['VALUE'],c=colors[i])
plt.show()

我们把数据通过不同的颜色的线条画出来了,但是不知道每条线代表的是什么意思。下面我们给折线图加上一个图例:

fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
label = str(1948 + i)
plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)
plt.legend(loc='best')
plt.show()

我们通过label=label 设置好一个label为图例,然后进行设置,默认为plt.legend(loc='best')图例显示在右边

我们做下点单的修改,让整个折线图完整标准的显示

fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
label = str(1948 + i)
plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)
plt.legend(loc='upper left')
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')
plt.show()

我们增加了整个折线图的XY轴表示,图例位置调整为左边。添加了折线图的标题。

今天讲解就先到这里,我们可以看到,通过Matplotlib库,可以很轻松的画出我们想要的图。

感谢各位的阅读,谢谢~~

Python数据可视化库-Matplotlib(二)的更多相关文章

  1. Python数据可视化库-Matplotlib(一)

    今天我们来学习一下python的数据可视化库,Matplotlib,是一个Python的2D绘图库 通过这个库,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率图,条形图,错误图,散点图等等 废 ...

  2. Python数据可视化——使用Matplotlib创建散点图

    Python数据可视化——使用Matplotlib创建散点图 2017-12-27 作者:淡水化合物 Matplotlib简述: Matplotlib是一个用于创建出高质量图表的桌面绘图包(主要是2D ...

  3. python的数据可视化库 matplotlib 和 pyecharts

    Matplotlib大家都很熟悉    不谈. ---------------------------------------------------------------------------- ...

  4. python 数据可视化(matplotlib)

    matpotlib 官网 :https://matplotlib.org/index.html matplotlib 可视化示例:https://matplotlib.org/gallery/inde ...

  5. Python数据可视化之Matplotlib实现各种图表

    数据分析就是将数据以各种图表的形式展现给领导,供领导做决策用,因此熟练掌握饼图.柱状图.线图等图表制作是一个数据分析师必备的技能.Python有两个比较出色的图表制作框架,分别是Matplotlib和 ...

  6. Python数据可视化利器Matplotlib,绘图入门篇,Pyplot介绍

    Pyplot matplotlib.pyplot是一个命令型函数集合,它可以让我们像使用MATLAB一样使用matplotlib.pyplot中的每一个函数都会对画布图像作出相应的改变,如创建画布.在 ...

  7. Python数据可视化之matplotlib

    常用模块导入 import numpy as np import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot ...

  8. python数据可视化(matplotlib)

  9. python数据可视化-matplotlib入门(7)-从网络加载数据及数据可视化的小总结

    除了从文件加载数据,另一个数据源是互联网,互联网每天产生各种不同的数据,可以用各种各样的方式从互联网加载数据. 一.了解 Web API Web 应用编程接口(API)自动请求网站的特定信息,再对这些 ...

随机推荐

  1. C#拷贝整个文件夹以及子目录和其中文件

       private void CopyDirectory(string srcPath, string desPath)         {             string folderNam ...

  2. Python转载

    让Python的经验更多一点 Python while 1 和 while True 速度比较 Python %s和%r的区别

  3. 谷歌编码风格内容,新建一个xml文件,复制进去就可以在eclipse里面用了,命名--eclipse-java-google-style.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...

  4. HTTP提交方式之PUT详细介绍及POST和PUT的区别

    Http定义了与 服务器的交互方法,其中除了一般我们用的最多的GET,POST 其实还有PUT和DELETE 根据RFC2616标准(现行的HTTP/1.1)其实还有OPTIONS,GET,HEAD, ...

  5. C. Alyona and mex

    http://codeforces.com/contest/740/problem/C 构造思维题. 第一直觉就是区间长度+1的最小值就是答案. 然而不知道怎么去构造这个序列. 其实就是每个区间,都要 ...

  6. 动手实现 React-redux(一):初始化工程

    可以看到 Redux 并不复杂,它那些看起来匪夷所思的设定其实都是为了解决特定的问题而存在的,我们把问题想清楚以后就不难理解它的那些奇怪的设定了.这节开始我们来看看如何把 Redux 和 React. ...

  7. 关于发布WP 8.1应用信息不匹配问题的解决办法

    错误提示:   与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...

  8. 5款好用的mysql客户端

    1. EMS SQL Manager for MySQL 是一款高性能MySQL数据库服务器系统的管理和开发工具.它支持从MySQL 3.23到6.0的任一版本,并支持最新版本的MySQL的特点,包括 ...

  9. Qt中为QPushButton添加背景图片

    有2种方式,一种是在代码中设置,另外一种是直接在Qt Creator中直接设置,下面是第二种 参考: http://doc.qt.io/qt-4.8/stylesheet-examples.html ...

  10. 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC

    Coding Tree Depth Estimation for Complexity Reduction of HEVC <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见: ...