使用matplotlib 2D等高线图添加额外的等高线 [英] Adding extra contour lines using matplotlib 2D contour plotting
问题描述
我正在用matplotlib创建一个二维轮廓图.使用提供的文档 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')
输出以下图.
文档详细介绍了如何在现有图上手动标记某些轮廓(或线").我的问题是如何创建比所示轮廓线更多的轮廓线.
例如,所示的图具有两个二元高斯.右上角有三个轮廓线,分别位于0.5
,1.0
和1.5
.
如何在0.75
和1.25
处添加轮廓线?
此外,我应该能够放大并(原则上)从(例如)1.0
和1.5
添加数十个轮廓线.如何做到这一点?
要绘制指定级别的等值线,请设置此处的第六张图使用此方法在levels = np.arange(-1.2, 1.6, 0.2)
处绘制等值线. /p>
要放大,请设置所需区域的x
限制和y
限制:
plt.xlim(0, 3)
plt.ylim(0, 2)
并绘制例如24个自动选择的关卡,请使用
CS = plt.contour(X, Y, Z, 24)
例如,
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()
此处的第三幅图使用此方法绘制了6条等值线.
I am creating a two-dimensional contour plot with matplotlib. Using the documentation provided http://matplotlib.org/examples/pylab_examples/contour_demo.html, such a contour plot can be created by
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
.
How could I add contour lines at say 0.75
and 1.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
. How does one do this?
To draw isolines at specified level values, set the levels
parameter:
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)
.
To zoom in, set the x
limits and y
limits of the desired region:
plt.xlim(0, 3)
plt.ylim(0, 2)
and to draw, say, 24 automatically-chosen levels, use
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.
这篇关于使用matplotlib 2D等高线图添加额外的等高线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!