如何使用Matplotlib颤动刻度 [英] how to use matplotlib quiver scale

查看:40
本文介绍了如何使用Matplotlib颤动刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一系列矢量绘图.我可以使用matplotlib的颤抖例程获取任意数量的绘图.问题是,颤动器会自动缩放每个图,但是我需要每个图中的向量都代表相同的比例.例如,如果 10 公里/小时在一个图中用 1 厘米的向量表示,那么 10 公里/小时应该在所有图中用 1 厘米的向量表示.(我真的不在乎矢量是否特别是 1cm.那只是一个例子.)我想我可以通过为每个图分别调整 scale 参数来实现这一点.但这似乎不起作用.

I need to do a series of vector plots. I can get any number of plots with matplotlib's quiver routine. The thing is, quiver autoscales each plot, but I need the vectors in each plot to all represent the same scale. For instance, if 10 km/hr is represented by a vector of 1cm in one plot, then 10km/hr should be represented by a 1cm vector in all plots. (I don't really care if the vector is specifically 1cm. That's just an example.) I thought I could make this happen by adjusting the scale argument separately for each plot. But it doesn't seem to work.

例如,我在第一个情节 mxs1 中找到了最大速度,然后对于每个情节我都做了类似的事情

For example, I find the maximum speed in the first plot, mxs1, and then for each plot I do something like

mxspd = np.max(speed[n])
pylab.quiver(x,y,vx[n],vy[n],scale=mxs1/mxspd)

但这并不足以调整向量的长度.例如,在我尝试的情况下, mxspd 大约是 mxs1 的一半,因此情节 n 中的向量应大约是与第一个情节中的一样.但是,两个图中的向量的长度几乎相同.

But this does not adjust the lengths of the vectors enough. For instance, in the case I was trying, mxspd is about one half of mxs1, so the vectors in plot n should be about half as long as the ones in the first plot. But the vectors in the two plots have pretty much the same lengths.

推荐答案

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

x, y = np.mgrid[0:20, 0:25]
u = np.sin(2 *x * np.pi / 20)
v = np.cos(2 * y * np.pi / 25)

fig, (ax_l, ax_r) = plt.subplots(1, 2, figsize=(8, 4))

ax_r.quiver(x, y, u, v, scale=5, scale_units='inches')
ax_l.quiver(x, y, 2*u, 2*v, scale=5, scale_units='inches')

ax_l.set_title('2x')
ax_r.set_title('1x')

有关 scale scale_units kwargs.

See the documentation for explainations of the scale and scale_units kwargs.

这篇关于如何使用Matplotlib颤动刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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