如何设置为默认的matplotlib等高线图以始终标记等高线 [英] How to set as default matplotlib contour plot to always label contours

查看:143
本文介绍了如何设置为默认的matplotlib等高线图以始终标记等高线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我在文档中缺少一些明显的东西

Perhaps I'm missing something obvious in the documentation,

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

但是当我第一次创建轮廓图时,每个轮廓线都有标签。但是,默认情况下,matplotlib不会执行此操作。使用演示中给出的图,我在 0.00 3.00 之间生成了更多轮廓线:

but when I first create a contour plot, there are labels for each contour line. However, matplotlib does not do this by default. Using the plot given in the demo, I generate more contour lines between 0.00 and 3.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()

输出

每条轮廓线均已明确标记。现在,让我们放大该轮廓的不同区域,即((0.5,1.0),(0.5,1.0))

Each contour line is clearly labeled. Now, let's zoom in on a distinct region of this contour, i.e. ((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()

此输出显然未标记。

如何设置 plt.contour 自动标记每条轮廓线?

How can I set plt.contour to automatically label each and every contour line?

推荐答案

您可能需要像这样直接更改x和y:

You probably need to change x and y directly like so:

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

这篇关于如何设置为默认的matplotlib等高线图以始终标记等高线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆