[英]Setting arbitrary axis value for a contour plot of form (x,y,f(x,y))?
So I have a data set that is in the matrix form: 所以我有一个矩阵形式的数据集:
x1, Y1, VALUE1
x2, Y1, VALUE2
x3, Y1, VALUE3
x1, Y2, VALUE4
x2, Y2, VALUE5
x3, Y2, VALUE6
and so on. 等等。 I get my contours properly except my x and y axes go from say 1, 2, 3...N.
我的轮廓正确,但我的x轴和y轴分别来自1、2、3 ... N。 This is fine because it is representing pixels so isn't incorrect, but I would like to change the axes values from pixels to the actual units.
这很好,因为它表示像素,所以这是不正确的,但是我想将轴值从像素更改为实际单位。 I can't seem to find a way to instruct contour to allow me to add this.
我似乎找不到找到指示轮廓的方法来允许我添加它。
bsquare=np.reshape(value,(x length,y length))
blue=contour(bsquare,colors='b')
plt.show()
where xlength and ylength are the number of points in either axis. 其中xlength和ylength是任一轴上的点数。
plt.contour
can be given arrays X, Y, Z
then it takes the Z
as the contour values and the X
and Y
are used on their respective axes. 可以给
plt.contour
数组X, Y, Z
然后将Z
作为轮廓值,并在各自的轴上使用X
和Y
Here is a script that first makes some data to play with, then gets into an array of the form you describe: 这是一个脚本,该脚本首先处理一些数据,然后进入您描述的形式的数组:
import matplotlib.pyplot as plt
import numpy as np
# Make some test data
nx = 2
ny = 3
x = np.linspace(0, 3, nx)
y = np.linspace(50, 55, ny)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + Y
# Now get it into the form you describe
data = [[[x[i], y[j], Z[j, i]] for i in range(nx)] for j in range(ny)]
data = np.array(data)
print data
>>>
[[[ 0. 50. 50. ]
[ 3. 50. 50.14112001]]
[[ 0. 52.5 52.5 ]
[ 3. 52.5 52.64112001]]
[[ 0. 55. 55. ]
[ 3. 55. 55.14112001]]]
Note I am using a numpy.array
not just a normal list this is important in the next step. 注意我使用的是
numpy.array
而不是普通列表,这在下一步很重要。 Lets split up that data as I presume you have done into the x and y values and the values themselves: 让我们像假定您已经将其拆分为x和y值以及这些值本身一样:
# Now extract the data
x_values = data[:, :, 0]
y_values = data[:, :, 1]
values = data[:, :, 2]
Now all of these things are nx, ny
arrays, that is they have the same shape as your bsquare
. 现在所有这些都是
nx, ny
数组,也就是说它们的形状与bsquare
相同。 You can check this by printing values.shape
and changing the integers nx, ny
. 您可以通过打印
values.shape
并更改整数nx, ny
。 Now I will plot three things: 现在,我将介绍三件事:
Firstly as you have done simply contour plot the values, this automatically adds the axes values 首先,只需简单地绘制等高线值即可,这会自动添加轴值
Secondly I plot using the arrays to give the correct scalings and 其次,我使用数组进行绘图以给出正确的缩放比例并
Finally I will plot the origin data set to show it properly recovers the data. 最后,我将绘制原始数据集,以显示它可以正确恢复数据。
You will need to compare the axis values with where the fake data was created: 您需要将轴值与创建伪数据的位置进行比较:
fig, axes = plt.subplots(ncols=3, figsize=(10, 2))
axes[0].contour(values)
axes[1].contour(x_values, y_values, values)
axes[2].contour(X, Y, Z)
How you implement this will largely depend on how you have imported your data. 如何执行此操作很大程度上取决于您导入数据的方式。 If you can simply turn it into a
numpy.array()
then I think this will solve your issue. 如果您可以简单地将其转换为
numpy.array()
那么我认为这将解决您的问题。
Hopefully I understood your problem correctly. 希望我能正确理解您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.