简体   繁体   English

奇怪的 matplotlib 轮廓 plot 轴标签显示两组值

[英]Weird matplotlib contour plot axis labels shows two sets of values

I'm using matplotlib 3.3.4 and generating a basic contour plot.我正在使用 matplotlib 3.3.4 并生成基本轮廓 plot。 But when I do so, the X and Y axis labels are showing my desired range (eg 0 to pi) but there's also an extra set of labels showing up that appear to be some sort of normalized values (0 to 1).但是当我这样做时,X 和 Y 轴标签显示了我想要的范围(例如 0 到 pi),但还有一组额外的标签显示出来,似乎是某种标准化值(0 到 1)。 The following code reproduces this:以下代码重现了这一点:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, np.pi, 40)
y = np.linspace(0, np.pi, 40)
z = np.sin(x[:, None])**2 + np.sin(y)**2

fig, ax = plt.subplots(figsize=(10,10))
ax = fig.add_subplot(111)
ax.contour(x, y, z)

and produces a plot like the one below.并产生如下图所示的 plot。 I see axis labels at the expected values [0, 0.5, 1.0, 1.5, 2.0, 2.5, and 3.0].我在预期值 [0、0.5、1.0、1.5、2.0、2.5 和 3.0] 处看到轴标签。 But there's another set [0, 0.2, 0.4, 0.6, 0.8, 1.0] that's coming from somewhere.但是还有另一组 [0, 0.2, 0.4, 0.6, 0.8, 1.0] 来自某个地方。

After reviewing the contour() example at Contour Example I realize I should probably be calling np.meshgrid() rather than doing the extra axis stuff to produce z above.在查看Contour Example中的 contour() 示例后,我意识到我可能应该调用 np.meshgrid() 而不是执行额外的轴操作来生成上面的 z。

Any clues as to what is causing this odd axis label behavior?关于是什么导致这个奇怪的轴 label 行为的任何线索? 在此处输入图像描述

You're adding two subplots, one via plt.subplots , and one via add_subplot .您正在添加两个子图,一个通过plt.subplots ,一个通过add_subplot Remove one of them and the figure will only have one set of ticks:删除其中一个,图形将只有一组刻度:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, np.pi, 40)
y = np.linspace(0, np.pi, 40)
z = np.sin(x[:, None])**2 + np.sin(y)**2

fig, ax = plt.subplots(figsize=(10,10))
ax.contour(x, y, z)

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

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