简体   繁体   English

使用matplotlib 2D轮廓绘图添加额外的轮廓线

[英]Adding extra contour lines using matplotlib 2D contour plotting

I am creating a two-dimensional contour plot with matplotlib. 我正在用matplotlib创建一个二维等高线图。 Using the documentation provided http://matplotlib.org/examples/pylab_examples/contour_demo.html , such a contour plot can be created by 使用http://matplotlib.org/examples/pylab_examples/contour_demo.html提供的文档,可以创建这样的等高线图

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')

which outputs the following plot. 输出以下图表。 在此输入图像描述

The documentation details how to manually label certain contours (or "lines") on the existing plot. 该文档详细说明了如何在现有绘图上手动标记某些轮廓(或“线”)。 My question is how to create more contour lines than those shown. 我的问题是如何创建比所示更多的轮廓线。

For example, the plot shown has two bivariate gaussians. 例如,显示的图有两个双变量高斯。 The upper right has three contour lines, at 0.5 , 1.0 , and 1.5 . 右上角有三条轮廓线,分别0.5 1.01.5

How could I add contour lines at say 0.75 and 1.25 ? 如何在0.751.25处添加轮廓线?

Also, I should be able to zoom in and (in principle) add dozens of dozens of contour lines from (for example) 1.0 and 1.5 . 此外,我应该能够放大并(原则上)从(例如) 1.01.5添加几十个等高线。 How does one do this? 怎么做到这一点?

To draw isolines at specified level values, set the levels parameter : 要以指定的级别值绘制等值线,请设置levels参数

levels = np.arange(-1.0,1.5,0.25)
CS = plt.contour(X, Y, Z, levels=levels)

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()
levels = np.arange(-1.0,1.5,0.25)
CS = plt.contour(X, Y, Z, levels=levels)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('levels = {}'.format(levels.tolist()))
plt.show()

在此输入图像描述

The sixth figure here uses this method to draw isolines at levels = np.arange(-1.2, 1.6, 0.2) . 这里的第六个数字使用此方法绘制levels = np.arange(-1.2, 1.6, 0.2)等值线。


To zoom in, set the x limits and y limits of the desired region: 要放大,请设置所需区域的x限制和y限制:

plt.xlim(0, 3)
plt.ylim(0, 2)

and to draw, say, 24 automatically-chosen levels, use 并且,例如,绘制24个自动选择的级别,使用

CS = plt.contour(X, Y, Z, 24)

For example, 例如,

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()
N = 24
CS = plt.contour(X, Y, Z, N)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('{} levels'.format(N))
plt.xlim(0, 3)
plt.ylim(0, 2)
plt.show()

在此输入图像描述

The third figure here uses this method to draw 6 isolines. 这里的第三个数字使用这种方法绘制6个等值线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM