自定义Python imshow(matplotlib)的刻度标记。

huangapple go评论58阅读模式
英文:

Custom tickmarks for python imshow (matplotlib)

问题

以下是您要翻译的内容:

我想要在以下plt.imshow()绘图中添加自定义刻度标记(如下所示):

import numpy as np
import matplotlib.pyplot as plt

zmm = np.linspace(0,121,121).reshape(11,11)
plt.imshow(zmm, cmap = 'Blues')
plt.show()

自定义Python imshow(matplotlib)的刻度标记。

我尝试使用以下方式添加所需的自定义刻度标记:

xticks = np.linspace(6.5,7.5,6)
yticks = np.linspace(.14,.18,6)

plt.imshow(zmm, cmap = 'Blues')
plt.xticks(xticks)
plt.show()

下面的图显示了结果(尝试x和yticks均产生更糟糕的结果)。我无法通过这个参考链接解决这个问题。

自定义Python imshow(matplotlib)的刻度标记。

英文:

I want to add custom tickmarks to the following plt.imshow() plot (see below):

import numpy as np
import matplotlib.pyplot as plt

zmm = np.linspace(0,121,121).reshape(11,11)
plt.imshow(zmm, cmap = 'Blues')
plt.show()

自定义Python imshow(matplotlib)的刻度标记。

I tried to add the custom tickmarks that I need with:

xticks = np.linspace(6.5,7.5,6)
yticks = np.linspace(.14,.18,6)

plt.imshow(zmm, cmap = 'Blues')
plt.xticks(xticks)
plt.show()

The figure below shows the outcome (trying for both x and yticks conveys even worse results). I could not solve that with this reference.

自定义Python imshow(matplotlib)的刻度标记。

答案1

得分: 2

IIUC,您希望使用xticksyticks的值重新标记刻度线(0..10)。重要的是要了解刻度线(位置)和标签(文本)之间的区别。请参阅文档

在您的情况下,刻度线将保持在相同的位置,只有标签将发生更改:

import numpy as np
import matplotlib.pyplot as plt

zmm = np.linspace(0, 121, 121).reshape(11, 11)
plt.imshow(zmm, cmap='Blues')

nb_ticks = 6

xticks = np.linspace(6.5, 7.5, nb_ticks)
yticks = np.linspace(.14, .18, nb_ticks)

new_xticks = np.linspace(0, 10, nb_ticks)
new_yticks = np.linspace(0, 10, nb_ticks)

plt.xticks(new_xticks, labels=xticks)
plt.yticks(new_yticks, labels=yticks)

plt.show()

输出:

自定义Python imshow(matplotlib)的刻度标记。

英文:

IIUC you want to relabel the ticks (0..10) with the values of xticks and yticks. It's important to know the difference between a tick (the position) and a label (the text). See the doc.

In your case, the ticks will stay at the same position, while only the labels will change:

import numpy as np
import matplotlib.pyplot as plt

zmm = np.linspace(0,121,121).reshape(11,11)
plt.imshow(zmm, cmap = 'Blues')

nb_ticks = 6

xticks = np.linspace(6.5, 7.5, nb_ticks)
yticks = np.linspace(.14, .18, nb_ticks)

new_xticks = np.linspace(0, 10, nb_ticks)
new_yticks = np.linspace(0, 10, nb_ticks)

plt.xticks(new_xticks, labels=xticks)
plt.yticks(new_yticks, labels=yticks)

plt.show()

Output:

自定义Python imshow(matplotlib)的刻度标记。

答案2

得分: 1

或者,如果这实际上不是一张图片,你可能要考虑使用 pcolormesh,它需要 xy 值,这些值用来确定刻度。imshow 使用与数组中的索引相对应的刻度(如果你不传递 x 和 y 点,pcolormesh 也是如此)。为了使你的代码工作,我改变了 xticksyticks 中的点数,因为刻度的数量和行/列(取决于 x 还是 y)之间必须有一对一的对应关系。

此外,请注意,pcolormesh 不像 imshow 那样默认为正方形图,因此添加了一行代码来使它成为正方形。

import numpy as np
import matplotlib.pyplot as plt

xticks = np.linspace(6.5, 7.5, 11)
yticks = np.linspace(0.14, 0.18, 11)

zmm = np.linspace(0, 121, 121).reshape(11, 11)
plt.pcolormesh(xticks, yticks, zmm, cmap='Blues')
plt.gca().set_box_aspect(1)
plt.show()

自定义Python imshow(matplotlib)的刻度标记。

你还会注意到,pcolormesh 将原点放在左下角(像典型的图表),而不是在左上角,这是 imshow 所做的。

英文:

Alternatively, if this is not actually an image, you might want to consider using pcolormesh, which takes x and y values, which are used to determine the ticks. imshow uses ticks corresponding to the indices in the array (which pcolormesh also does if you don't pass the x and y points). To make your code work, I changed the number of points in xticks and yticks since there needs to be a one-to-one correspondence for the number of ticks and the rows/columns (depending on x or y).

Also, note that pcolormesh doesn't default to square plots like imshow, hence the added line to make it square.

import numpy as np
import matplotlib.pyplot as plt

xticks = np.linspace(6.5, 7.5, 11)
yticks = np.linspace(0.14, 0.18, 11)

zmm = np.linspace(0, 121, 121).reshape(11, 11)
plt.pcolormesh(xticks, yticks, zmm, cmap='Blues')
plt.gca().set_box_aspect(1)
plt.show()

自定义Python imshow(matplotlib)的刻度标记。

You'll also note that pcolormesh places the origin in the bottom left (like a typical graph) rather than in the top left, which imshow does.

huangapple
  • 本文由 发表于 2023年7月20日 19:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定