使用Matplotlib绘制流线-Python [英] Plotting streamlines with Matplotlib - Python

查看:83
本文介绍了使用Matplotlib绘制流线-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Matplotlib绘制一些流线型.

I'm trying to plot some streamlines with Matplotlib.

到目前为止,我有这段代码,例如绘制 10 x 10 矢量场的示例:

I have this code so far, as an example to plot a 10 x 10 vector field:

def plot_streamlines(file_path, vector_field_x, vector_field_y):
    plt.figure()
    y, x = numpy.mgrid[-2:2:10j, -2:2:10j]
    plt.streamplot(x, y, vector_field_x, vector_field_y, color='y', cmap=plt.cm.autumn)
    plt.savefig(file_path + '.png')
    plt.close()

这可以正常工作,但如果我只是更改这一行:

This works properly, but if I just change this line:

y, x = numpy.mgrid[-2:2:10j, -2:2:10j]

给那个:

x, y = numpy.mgrid[-2:2:10j, -2:2:10j]

我遇到了一些错误:

Traceback (most recent call last):
  File "Library/Python/2.7/lib/python/site-packages/matplotlib/pyplot.py", line 3224, in streamplot minlength=minlength, transform=transform)
  File "/Library/Python/2.7/lib/python/site-packages/matplotlib/axes.py", line 6861, in streamplot transform=transform)
  File "Library/Python/2.7/lib/python/site-packages/matplotlib/streamplot.py", line 67, in streamplot grid = Grid(x, y)
  File "Library/Python/2.7/lib/python/site-packages/matplotlib/streamplot.py", line 256, in __init__
    assert np.allclose(x_row, x)
AssertionError

我不知道如何使用标准"订单 x/y ,因为我的网格是正方形的.此外,如果我的 xy 尺寸相同,我不知道我是如何得到这些错误的.

I didn't understand how I can use the "standard" order x / y, since my mesh grid is squared. Moreover, I don't know how I get these erros if my x and y dimensions are the same.

任何帮助将不胜感激.

谢谢.

推荐答案

pylab示例使用 Y,X ,然后使用numpy.mgrid绘制流图的X,Y.

The pylab examples use Y,X then plot X,Y for streamplots using numpy.mgrid.

import numpy as np
import matplotlib.pyplot as plt

Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
plt.colorbar()

f, (ax1, ax2) = plt.subplots(ncols=2)
ax1.streamplot(X, Y, U, V, density=[0.5, 1]    
lw = 5*speed/speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)

plt.show()

来自此处.

y,x = numpy.mgrid[-2:2:4j,-2:2:4j]
x = [[-2.         -0.66666667  0.66666667  2.        ]
    [-2.         -0.66666667  0.66666667  2.        ]
    [-2.         -0.66666667  0.66666667  2.        ]
    [-2.         -0.66666667  0.66666667  2.        ]]


y = [[-2.         -2.         -2.         -2.        ]
     [-0.66666667 -0.66666667 -0.66666667 -0.66666667]
     [ 0.66666667  0.66666667  0.66666667  0.66666667]
     [ 2.          2.          2.          2.        ]]

关于数据在x和y轴(-2,2等)方面的外观似乎有直接关系.类似于x轴,而y值类似于y轴.

There seems a direct relation with how the data looks in regard to x and y axis, the -2,2 etc.. resembling an x-axis and y values resembling a y-axis.

这篇关于使用Matplotlib绘制流线-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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