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. Linux性能优化实战学习笔记:第四十三讲

    一.上节回顾 上一节,我们了解了 NAT(网络地址转换)的原理,学会了如何排查 NAT 带来的性能问题,最后还总结了 NAT 性能优化的基本思路.我先带你简单回顾一下. NAT 基于 Linux 内核 ...

  2. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  3. oracle--介质恢复和实例恢复的基本概念

    1.概念 REDO LOG是Oracle为确保已经提交的事务不会丢失而建立的一个机制.实际上REDO LOG的存在是为两种场景准备的,一种我们称之为实例恢复(INSTANCE RECOVERY),一种 ...

  4. idea找不到tomcat,找不到Tomcat server

    打开settings  添加即可 如果再没有,可能需要先安装timcat插件

  5. 第31课 std::atomic原子变量

    一. std::atomic_flag和std::atomic (一)std::atomic_flag 1. std::atomic_flag是一个bool类型的原子变量,它有两个状态set和clea ...

  6. 实战django(二)--登录实现记住我

    上节初步实现了登录和注册模块,这节我们进一步实现“记住我”功能. 大体功能分为以下模块: 1.在登录时如果勾选记住我,那么就将用户username存进cookie中,跳转到index页面: 2.此时, ...

  7. Thread.currentThread().setContextClassLoader为什么不生效与java.lang.NoClassDefFoundError之Java类加载的Parent first Classloader

    众所周知,Java的类加载机制采用了双亲委派模型,导致在进行类加载的时候会有多个加载器,这种复杂的机制,有时候会导致‘Exception in thread main java.lang.NoClas ...

  8. ant-design自定义FormItem--上传文件组件

    自定义上传组件,只需要在内部的值变化之后调用props中的onChange方法就可以托管在From组件中, 此外为了保证,初始化值发生变化后组件也发生变化,需要检测initialValue 变化,这是 ...

  9. why’s kafka so fast

    As we all know that Kafka is very fast, much faster than most of its competitors. So what’s the reas ...

  10. Springboot2+SpringSecurity+Oauth2+Mysql数据库实现持久化客户端数据

    目录 介绍 建表,初始化数据 工程配置 Authorization Server - Spring Security配置 Authorization Server - 授权服务器 Resource S ...