难以在python中绘制显示当前速度和方向VS深度的图表

时间:2017-08-22 10:18:54

标签: python matplotlib

我很难在python中绘制显示当前速度和方向VS深度的图表我尝试了以下代码,但是所有箭头都是单色的,如果更改速度值,箭头的大小太大,我会不希望在x轴上显示任何东西,但是在下面的代码中,需要在下面设置xlim示例速度和方向使用

转换为u和v
<div class='scrolltop'>
    <div class='scroll icon'><i class="fa fa-4x fa-angle-up"></i></div>
</div>
<script>
$(window).scroll(function() {
    if ($(this).scrollTop() > 50 ) {
        $('.scrolltop:hidden').stop(true, true).fadeIn();
    } else {
        $('.scrolltop').stop(true, true).fadeOut();
    }
});
$(function(){$(".scroll").click(function(){$("html,body").animate({scrollTop:$(".thetop").offset().top},"1000");return false})})
</script>
<?php wp_footer(); ?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望所有速度矢量指示当前的运动方向,但长度相同。要实现这一点,您必须先将速度向量标准化,然后再将其传递给quiver。对于向量的颜色编码(表示绝对速度值),语法有一个小问题。请参阅下面的代码。我还添加了一些代码,用于向情节添加colorbar(基于this answer)。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm

data=[(0, 1.95, -0.8159606717671093, 2.2197413061368696), (0, 2.95, -0.3582079405727933, 1.2392687647603318), (0, 3.95, -0.31686646083755926, 1.291702615154231), (0, 4.95, -0.29746052030193276, 1.2860471371072304), (0, 5.95, -0.26344777968183913, 1.262812443469223), (0, 6.95, -0.23878868113642596, 1.2065985105912929), (0, 7.95, -0.25428152412858535, 1.1727492939613495), (0, 8.95, -0.23806795164869818, 1.1148648574593214), (0, 9.95, -0.17646920235662847, 1.1060102262726217), (0, 10.95, -0.18770370727477204, 1.0838668360436658), (0, 11.95, -0.17964112037348914, 1.054812337750634), (0, 12.95, -0.16791299403578752, 1.0060841050498401)]
data = np.array(data, dtype=[('x', np.float32), ('y', np.float32), ('u',   np.float32), ('v', np.float32)])


fig,ax = plt.subplots()

##computing absolute speed value and color scale
##ajust min_speed and max_speed as needed
speed = np.sqrt(data['u']**2+data['v']**2)
min_speed = np.min(speed)
max_speed = np.max(speed)
scale = (speed-min_speed)/(max_speed-min_speed)

##normalizing the speed components:
##adjust the vector length with fac
fac = 1.0
u_norm = fac*data['u']/speed
v_norm = fac*data['v']/speed

##the colormap:
cmap = cm.get_cmap('rainbow')
cs = cmap(scale)

##the arrows
ax.quiver(data['x'], data['y'], u_norm, v_norm, angles='uv',     scale_units='xy', scale=1,color=cs,pivot='mid')
ax.set_xlim([-1, 10])

##the colorbar (from https://stackoverflow.com/a/44328461/2454357):
sm = plt.cm.ScalarMappable(cmap=cmap)
sm.set_array(np.linspace(min_speed,max_speed,10))
fig.colorbar(sm)
plt.xticks(rotation=0)

plt.show()

结果如下:

result of the code sample

希望这有帮助。