2015-07-19 134 views
2

也许我错过了文档中的东西很明显,如何设置为默认matplotlib轮廓图总是标记轮廓

http://matplotlib.org/examples/pylab_examples/contour_demo.html

但是当我第一次创建的等高线图,对于每个轮廓标签线。但是,matplotlib默认不会这样做。使用在演示中给出的曲线图,我生成0.003.00之间更轮廓线:

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(0.00, 3.00, 0.25) 
CS = plt.contour(X, Y, Z, levels=levels) 
plt.clabel(CS, inline=1, fontsize=10) 
plt.xlim(0, 3) 
plt.ylim(0, 2) 
plt.show() 

其输出

enter image description here

每个轮廓线清楚地标明。现在,让我们放大这个轮廓鲜明的区域,即((0.5, 1.0), (0.5, 1.0))

plt.figure() 
levels = np.arange(0.00, 3.00, 0.25) 
CS = plt.contour(X, Y, Z, levels=levels) 
plt.clabel(CS, inline=1, fontsize=10) 
plt.xlim(0.5, 1.0) 
plt.ylim(0.5, 1.0) 
plt.show() 

这个输出显然是不标注。

enter image description here

如何设置plt.contour自动标示每一个轮廓线?

回答

1

你可能需要直接改变x和y像这样:

x = np.arange(0.5, 1.0, delta) 
y = np.arange(0.5, 1.0, delta) 
+0

另外,如果你玩弄在互动的情节放大,你可以看到为什么这是必要的。 'plt.contour'显然可以解决标签可以在哪里放置文本块,位置与数据相关,字体大小与坐标轴成比例。 – cphlewis