matplotlib轮廓输入数组顺序 [英] matplotlib contour input array order

查看:65
本文介绍了matplotlib轮廓输入数组顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:matplotlib的轮廓期望输入2D数组的顺序是什么?

Question: what order does the contour from matplotlib expect for the input 2D array?

详细说明:Matplotlib轮廓文档指出例程调用为

Elaborate: Matplotlib contour documentation says that the routine call is

x_axis = np.linspace(10,100,n_x)
y_axis = np.linspace(10,100,n_y)
matplotlib.pyplot.contour(x_axis, y_axis, scalar_field)

其中scalar_field必须是2D数组.例如,scalar_field可以由

Where scalar_field must be a 2D array. For example, the scalar_field can be generated by

scalar_field = np.array( [(x*y) for x in x_axis for y in y_axis])
scalar_field = scalar_field.reshape(n_x, n_y)

如果将scalar_field赋予轮廓,

If scalar_field is given to contour,

plt.contour(x_axis, y_axis,scalar_field) #incorrect

图的方向不正确(旋转).为了恢复正确的方向,必须对scalar_field进行转置:

the orientation of the plot is incorrect (rotated). To restore the proper orientation the scalar_field must be transposed:

plt.contour(x_axis, y_axis,scalar_field.transpose()) #correct

那么等值线期望scalar_field的顺序是什么?

So what is the order that contour expect that scalar_field has?

推荐答案

您应该使用contour进行绘制,同时传递XY的二维数组,然后scalar_field数组中的每个点都将对应到XY中的坐标(x, y).您可以使用numpy.meshgrid方便地创建XY:

You should plot using contour passing also 2-D arrays for X and Y, then each point in your scalar_field array will correspond to a coordinate (x, y) in X and Y. You can conveniently create X and Y using numpy.meshgrid:

import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(x_axis, y_axis, copy=False, indexing='xy')
plt.contour(X, Y, scalar_field)    

如果希望x坐标表示线并且y表示列,则可以将参数indexing更改为'ij',但是在这种情况下,必须使用ij索引来计算scalar_fied.

The argument indexing can be changed to 'ij' if you want the x coordinate to represent line and y to represent column, but in this case scalar_fied must be calculated using ij indexing.

这篇关于matplotlib轮廓输入数组顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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