# _*_coding:utf-8_*_
# /usr/bin/env python3
# Author:book Miki

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 20, 20)
y1 = x**2
y2 = x*2+2
# plt.figure(num=1)
# plt.plot(x, y1)
plt.figure(num=2)
# set line label
# plt.plot(x, y1, label='first line')
plt.plot(x, y2, color='blue', linewidth=15, linestyle='-', label='second line', zorder=1)
plt.ylim((-5, 5))
plt.xlim((-5, 5))
# plt.xticks(np.linspace(-2, 10, 7))
# plt.yticks([-50, 0, 50, 100, 150, 200, 250, 300, 350],
# ['is -50', 'none', 'is 50', 'is 100', 'is 150', 'is 200', 'is 250', 'is 300', 'is 350'])
plt.ylabel('I am y')
plt.xlabel('I am x')
ax = plt.gca()
ax.spines['right'].set_color('none') # 使用.spines设置边框
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 设置ticks标签 所有位置:left,right,both,default,none
ax.spines['left'].set_position(('data', 0)) # 设置位置 位置所有属性:outward,axes,data
ax.spines['bottom'].set_position(('data', 0))
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
# 在 plt 2.0.2 或更高的版本中, 设置 zorder 给 plot 在 z 轴方向排序
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.7, zorder=2))
plt.legend(loc='upper right')
x0 = 1
y0 = 2*x0+1
plt.scatter(x0, y0, s=50, c='b')
plt.plot([x0, x0], [0, y0], linestyle='--', color='blue')
# 添加标注
plt.annotate('2*%s + 1 = %s' % (x0, y0), xy=(x0, y0), xytext=(+30, -30),
textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle='->',connectionstyle="arc3,rad=.2"))
# 添加文本
plt.text(-2, 2, 'this is function of matpoltlib.pypolt', fontdict={'size': '16', 'color': 'red'}, zorder=1)

plt.show()

'''
1.plot 出图plot()先x 在y color linewidth 线宽 linestyle -是直线 --是虚线
2.figure 一个figure就是一张图figure下面的plot 都在一张图上显示
3.设置范围 xlim((star, end)) 或者ylim
4.设置标签 xlabel() 或者ylabel()
5.设置范围()原本范围不变与刻度 xticks([list]) 或者标签str代替number yticks([number list], [str list])
6.ax.yaxis.set_ticks_position('left') # 设置ticks标签 所有位置:left,right,both,default,none
7.在移动坐标之前我们要将top和right的设置成看不见
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
8.移动x轴和y轴的位置 ax.spines['bottom'].set_position(('data', 0))# 设置位置 位置所有属性:outward,axes,data
ax.spines['left'].set_position(('data', 0))
9.标签使用,需要在polt的时候加入参数label 之后plt.legend(loc='upper right')
我们在后面的时候难免要修改就可以 plt.legend(handles=[l1, l2], labels=['up', 'down'], loc='best')
参数有 'best' : 0,
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
10.plt.scatter(x0, y0, s=50, c='b') 画点
plt.plot([x0, x0], [0, y0], linestyle='--', color='blue') 将点相连成线
11.标注
plt.annotate('2*%s + 1 = %s' % (x0, y0), xy=(x0, y0), xytext=(+30, -30),
textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle='->',connectionstyle="arc3,rad=.2"))
12.文本注释
plt.text(x,y,text,fontdict={'size': 16, 'color': 'red'})
13.图层的上下啥的,例如两根直线交织在一起,在什么的就会遮挡下面的显示
自然,在我们的图中xaxis 与yxis上的label可能会被线遮住
我们就可以通过改变zorder=1 或者 2 或者31这种输在来改变和设置透明度
plt.plot(x, y2, color='blue', linewidth=15, linestyle='-', label='second line', zorder=1) 将plot出的线放在1
plt.text(-2, 2, 'this is function of matpoltlib.pypolt', fontdict={'size': '16', 'color': 'red'}, zorder=1)
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
# 在 plt 2.0.2 或更高的版本中, 设置 zorder 给 plot 在 z 轴方向排序
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.7, zorder=2))
依次将x和y的label放在zorder = 2上
14.画点,并为点符上颜色
import matplotlib.pyplot as plt
import numpy as np
n = 10
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
t = np.arctan2(x, y)
plt.scatter(x, y, s=25, c=t, alpha=0.5)

15. 等高线,
等高线是在一个xy之上有高度,也就是z轴,并且这z是二维的,
也就是说,x和y构成了一个网格,然后每个网格上的小正方形,都有了自己的高度凸起来了
我们更具这个高度在x,y的平面上,先画等高线,然后上色。根
据这个过程,
1.构成网格 n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-4, 4, n)
X, Y = np.meshgrid(x, y)
2.对应的等分上色 plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot) 8的意思是将所有的高度分成8分,
cmap是color map此时选择的是hot
3.画线 C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidths=.5) 注: f(x,y)是早已设置好的函数
4.在线的里面加标签(label) plt.clabel(C, inline=True, fontsize=10)

16. 3D图
3d图和等高线的原理是一样的在网格上有高度,但是我们要显示3D图就要借助matplotlib里面3d的显示库
其次我们要将需要3D展示出来的figure传入Axes3D中生成实例
fg = plt.figure()
ax = Axes3D(fg)
x = np.arange(-4, 4, 0.25)
y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(x, y)
r = np.sqrt(X**2+Y**2)
z = np.sin(r)
ax.plot_surface(X, Y, z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow')) # 此处的z也必须是meshgrid格式下生成的z
# ax.contour(X, Y, z, zdir='z', offset=-1, camp=plt.get_cmap('rainbow')) # 等高线
ax.contourf(X, Y, z, zdir='z', offset=-1, camp=plt.get_cmap('rainbow')) # 等高图
注:rstride与cstride分别表示线的行幅度与列幅度
并且我们根据生成的3d数据画出对应的等高线图也就是ax.contourf (填充颜色)
17. subplot 多合一显示
导入模块matplotlib之后我们可以在生成的figure里面将figure使用subplot将其分成多个层次几行几列
需要注意的是 此时的subplot如plot一般一个figure下面的plot都在一个数据里面
plt.figure()
# 均匀显示
plt.subplot(2, 2, 1)
plt.plot([0, 5], [0, 5])
plt.subplot(2, 2, 2)
plt.plot([5, 5], [3, 1])
# 不均匀显示
plt.subplot(2, 3, 4)
plt.plot([0, 5], [0, 5])
plt.subplot(2, 3, 5)
plt.plot([0, 5], [0, 5])
plt.show()
由此看来不均匀显示也是计算好的,并不是subplot就会将figure分割,而是在假设分割的基础上
在贴图,所以后面2行3列,就是在4位置

18. ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax1.plot([1, 2], [1, 2]) # 画小图
ax1.set_title('ax1_title') # 设置小图的标题
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1)) # 分区 (2,1)是起始位置,默认colspan和rowspan是1
ax4.scatter([1, 2], [2, 2])
ax4.set_xlabel('ax4_x')
ax4.set_ylabel('ax4_y') # 设置xy的label
plt.tight_layout()
'''

图中图
  
'''
1.设置[left, botton, width, height]的比例生成实例,此比例为百分比
ax.add_axes([left, botton, width, height])
然后就可以plot了 也可以 ax.set_xlabel() 也可以 ax.sel_title

'''
import matplotlib.pyplot as plt
fig = plt.figure() left, botton, width, height = 0.1, 0.1, 0.8, 0.8
x = [0, 1, 5, 6, 4, 9]
y = [9, 10, 5, 8, 9, 6] ax = fig.add_axes([left, botton, width, height])
ax.plot(x, y, 'r')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('the one')
left, botton, width, height = 0.2, 0.6, 0.2, 0.2
ax1 = fig.add_axes([left, botton, width, height])
ax1.plot(x, y, 'b') plt.show()

python之《matplotlib》的更多相关文章

  1. Python利用pandas处理Excel数据的应用

    Python利用pandas处理Excel数据的应用   最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做 ...

  2. Python数据分析--Pandas知识点(三)

    本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) Python数据分析--Pandas知识点(二) 下面将是在知识点一, ...

  3. 基于 Python 和 Pandas 的数据分析(4) --- 建立数据集

    这一节我想对使用 Python 和 Pandas 的数据分析做一些扩展. 假设我们是亿万富翁, 我们会想要多元化地进行投资, 比如股票, 分红, 金融市场等, 那么现在我们要聚焦房地产市场, 做一些这 ...

  4. 基于 Python 和 Pandas 的数据分析(2) --- Pandas 基础

    在这个用 Python 和 Pandas 实现数据分析的教程中, 我们将明确一些 Pandas 基础知识. 加载到 Pandas Dataframe 的数据形式可以很多, 但是通常需要能形成行和列的数 ...

  5. 基于 Python 和 Pandas 的数据分析(1)

    基于 Python 和 Pandas 的数据分析(1) Pandas 是 Python 的一个模块(module), 我们将用 Python 完成接下来的数据分析的学习. Pandas 模块是一个高性 ...

  6. python安装pandas和lxml

    一.安装python 二.安装pip 三.安装mysql-connector(window版):下载mysql-connector-python-2.1.3,解压后进入目录,命令安装:pip inst ...

  7. python之pandas用法大全

    python之pandas用法大全 更新时间:2018年03月13日 15:02:28 投稿:wdc 我要评论 本文讲解了python的pandas基本用法,大家可以参考下 一.生成数据表1.首先导入 ...

  8. python之pandas简单介绍及使用(一)

    python之pandas简单介绍及使用(一) 一. Pandas简介1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据 ...

  9. Python数据分析--Pandas知识点(二)

    本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) 下面将是在知识点一的基础上继续总结. 13. 简单计算 新建一个数据表 ...

  10. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

随机推荐

  1. 发布MeteoInfo 1.2.5

    提升了MeteoInfoLab脚本数据处理能力,比如双Y轴图.多Y轴图.数组计算.坐标投影计算等.这里给出几个示例图,以后有空了会将示例脚本程序整理放在网上.坐标投影计算: 双Y轴图: 多Y轴图: 多 ...

  2. python算法常用技巧与内置库

    python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...

  3. pytest文档52-命令行参数--setup-show查看fixture的执行过程

    前言 使用命令行运行 pytest 用例的时候,看不到 fixture 的执行过程. 如果我们想知道fixture的执行过程和先后顺序,可以加上 --setup-show 命令行参数,帮助查看 fix ...

  4. canal 整合RabbitMQ

    环境如下: canal: 1.15-alpha-1 mysql  5.6.49 rabbitmq 3.7.14 Erlang 21.3 canal 安装和启动 见上篇文章 canal快速安装启动 但是 ...

  5. gin框架使用orm操作数据库(转)

      简介:orm俗称关系对象模型,用来映射数据库SQL和对象的工具 ,相当于mongodb里面的mongoose库,Java里面的mybatis ibatis Golang GORM使用 https: ...

  6. maven项目导入并运行

    idea导入maven工程流程 找到要导入的文件位置 打开导入 选择manve 一直next就好 选择jdk,选择自己的jdk--home就可以 点击finished 等待坐标导入,查看右侧maven ...

  7. CTF-pwn:老板,来几道简单pwn

    wdb_2018_3rd_soEasy 保护全关 在栈上写入shellcode,然后ret2shellcode from pwn import * local = 0pa binary = " ...

  8. Javascript中this作用域以及bind方法的重写

    这是一个最近遇到的笔试题,出于尊重,不会说出该公司的名字,源于自身比较少,笔试题是将bind方法用ES3重写,使用bind这个方法,导致一时半会懵了,只记得bind可以改变this的作用域. 作为查漏 ...

  9. (Pixel2PixelGANs)Image-to-Image translation with conditional adversarial networks

    Introduction 1. develop a common framework for all problems that are the task of predicting pixels f ...

  10. Pytorch中cudnn版本查询

    问题: Disable or able cudnn,查询版本. Disable cudnn for batch_norm: (See: @Microsoft / human-pose-estimati ...