繁体   English   中英

为轴设置范围和 label

[英]Set range and label for axis

我正在绘制大小为 10*10 的数组 A 中的数据,每个元素A[x,y]由 function f(x,y)计算,其中 x 和 y 在(-3, 3)范围内

import numpy as np
import matplotlib.pyplot as plt

def f(x,y):
    return ...

s = 10
a = np.linspace(-3, 3, s)
fxy = np.array([f(x,y) for x in a for y in a]).reshape((s, s))

plt.xticks(labels=np.arange(-3, 3), ticks=range(6))
plt.yticks(labels=np.arange(-3, 3), ticks=range(6))
plt.imshow(fxy)

在此处输入图像描述

所以 xy 轴上的标签不是我想要的,因为 x 和 y 取自范围(-3, 3) (而不是取自(0, 10) ,这是二维数组的大小)。 如何正确设置这些标签?

您可以在imshow中使用extent参数,它会自动处理刻度标签。

import numpy as np
import matplotlib.pyplot as plt

def f(x,y):
    return ...

s = 10
a = np.linspace(-3, 3, s)
fxy = np.array([f(x,y) for x in a for y in a]).reshape((s, s))

plt.imshow(fxy, extent = [a[0], a[-1], a[-1], a[0]])

暂无
暂无

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

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