contour - 绘制等高线

mp.contour(x, y, z, 等高线条数,colors=颜色, linewidth=线宽)#等高线绘制

contourf - 填充等高线

mp.contourf(x, y, z, 等高线条数,cmap=颜色映射)# 等高线填充

clabel - 标记等高线,向CS(由contour函数返回的matplotlib.contour.ContourSet对象)中的轮廓线添加标签

clabel(CS, *args, **kwargs) 
  • CS - 由contour函数产生的句柄对象
  • fontsize - string(smaller, x-large) or float ,optional
  • colors - Color of each label
    • None, 标记的颜色为轮廓的颜色
    • one string color (e.g color = 'r' ), 所有的标签均为红色
    • a tuple of matplotlib color args (string, float, rgb, etc), 不同的标签按照指定的颜色标记
  • inline - bool, optional . 默认True(在标签位值移除轮廓线,也即标签覆盖轮廓线,而非穿越)
  • inline_spacing - float, optional,默认5,放置内联时,标签两侧留有的像素空间
  • fmt - string or dict, optional, 默认'%1.3f ',保留小数位。1.3中的1表示输出位宽,3表示小数位长度,此时实际数据会覆盖掉该数据对应的轮廓线;当9.3时,轮廓线会被覆盖掉9个位置,同时小数点后保留3位,也就是说,轮廓线移除的长度大于数据长度
  • manual - bool or iterable, optional , 手动添加标签。忽略该字典
  • rightside_up - bool, optional,默认 True(标签旋转均以正负90度计)
  • use_clabeltext - bool, optional,默认False,若为True,则用 `ClabelText` class (instead of `Text`) 创建标签,当使用“CababelTress”绘制文本时,会重新计算文本的旋转角度,因此,如果轴的角度发生变化时,可以使用“CababelTress”来旋转角度。

举例:One-class SVM

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager
from sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))
# Generate train data
X = 0.3 * np.random.randn(100, 2)
X_train = np.r_[X + 2.4, X - 2.3]
X_test = np.r_[X + 2, X - 2]
# Generate some abnormal novel observations
X_outliers = np.random.uniform(low=0.1, high=4, size=(20, 2))
# fit the model
clf = svm.OneClassSVM(nu=0.1, kernel='rbf', gamma=0.1)
clf.fit(X_train)
y_pred_train = clf.predict(X_train)
y_pred_test = clf.predict(X_test)
y_pred_outliers = clf.predict(X_outliers)
n_error_train = y_pred_train[y_pred_train == -1].size
n_error_test = y_pred_test[y_pred_test == -1].size
n_error_outlier = y_pred_outliers[y_pred_outliers == 1].size # plot the line , the points, and the nearest vectors to the plane
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape) plt.title("Novelty Detection")
# 填充等高线图
plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu)
# 绘制等高线
a = plt.contour(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred') s = 40
b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='green', s=s, edgecolors='k')
b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='blueviolet', s=s, edgecolors='k')
c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='gold', s=s, edgecolors='k') plt.axis('tight')
plt.xlim((-5, 5))
plt.ylim((-5, 5))
plt.legend([a.collections[0], b1, b2, c],
["learned frontier", 'training observations',
"new regular observations", "new abnormal observations"],
loc="upper left",
prop=matplotlib.font_manager.FontProperties(size=11))
plt.xlabel("error train: %d/200; errors novel regular: %d/40; errors novel abnormal:%d/40" %
(n_error_train, n_error_test, n_error_outlier))
plt.show()

参考文献:

【1】基于matplotlib的数据可视化 - 等高线 contour 与 contourf

 

matplotlib---画等高线的更多相关文章

  1. matplotlib画图实例:pyplot、pylab模块及作图參数

    http://blog.csdn.net/pipisorry/article/details/40005163 Matplotlib.pyplot画图实例 {使用pyplot模块} matplotli ...

  2. MatplotLib常用基本操作

    本文记录matlibplot常用基本操作,都是基本功能,不涉及复杂联合操作,其中各用法详细用法可参考官网: 1. 基本画图操作 ##mofan_matplotlib.pyplot import mat ...

  3. matplotlib等高线图-【老鱼学matplotlib】

    等高线图是在地理课中讲述山峰山谷时绘制的图形,在机器学习中也会被用在绘制梯度下降算法的图形中. 因为等高线的图有三个信息:x, y以及x,y所对应的高度值. 这个高度值的计算我们用一个函数来表述: # ...

  4. 基于matplotlib的数据可视化 - 等高线 contour 与 contourf

    contour 与contourf 是绘制等高线的利器. contour  - 绘制等高线 contourf - 填充等高线 两个的返回值值是一样的(return values are the sam ...

  5. matplotlib绘制等高线图

    参考自Matplotlib Python 画图教程 (莫烦Python)(12)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...

  6. python+matplotlib 绘制等高线

    python+matplotlib 绘制等高线 步骤有七: 有一个m*n维的矩阵(data),其元素的值代表高度 构造两个向量:x(1*n)和y(1*m).这两个向量用来构造网格坐标矩阵(网格坐标矩阵 ...

  7. python之《matplotlib》

    # _*_coding:utf-8_*_# /usr/bin/env python3# Author:book Mikiimport matplotlib.pyplot as pltimport nu ...

  8. matplotlib的学习10-Contours 等高线图

    import matplotlib.pyplot as plt import numpy as np ''' 画等高线 数据集即三维点 (x,y) 和对应的高度值,共有256个点. 高度值使用一个 h ...

  9. python安装numpy、scipy和matplotlib等whl包的方法

    最近装了python和PyCharm开发环境,但是在安装numpy和matplotlib等包时出现了问题,现总结一下在windows平台下的安装方法. 由于现在找不到了工具包新版本的exe文件,所以采 ...

  10. matplotlib 高级用法实例--共享x轴

    http://localhost:8888/notebooks/duanqs/matplotlib_advanced_example.ipynb 我不会弄呀, 刚才从matplotlib文档里吧示例用 ...

随机推荐

  1. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  2. 树的遍历 | 1079 理解题意+DFS

    这题如果读懂了题意,就很好做,一波操作就结束了.不过题目有点难读,考验耐心和读题的细致. AC代码: #include <stdio.h> #include <memory.h> ...

  3. Linux性能优化实战学习笔记:第八讲

    一.环境准备 1.在第6节的基础上安装dstat wget http://mirror.centos.org/centos/7/os/x86_64/Packages/dstat-0.7.2-12.el ...

  4. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. IDEA中把普通的Java项目转换成gradle项目

    1.在该项目的跟目录下创建build.gradle 和 settings.gradle文件,内容如下: build.gradle: plugins { id 'java' } group 'coco' ...

  6. API加密框架原理解密

    关于框架的使用文章请参考: 前后端API交互如何保证数据安全性?:http://cxytiandi.com/blog/detail/20235 API数据加密框架monkey-api-encrypt: ...

  7. oracle--ORA-38760

    01,ORA-38760: This database instance failed to turn on flashback 02,问题处理思路 第一步:查看日志文件 查看这次启动的时候alter ...

  8. golang 1.12 自动补全

    golang 1.12 版本的自动补全问题 问题 golang 1.12 开始, 默认的 go install 不再生成 pkg 文件. 所以对第三方库的引用, 无法进行代码的自动补全. 解决方法 g ...

  9. dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gold;第一次无效

    private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {}//修改DataGrid ...

  10. 另一种缓存,Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...