matplotlib中带有叠加箭头的线图

时间:2018-06-26 15:14:41

标签: python matplotlib graph visualization seaborn

我正试图绘制全天的温度和风速与时间的关系。 我希望做的是将温度绘制为法线图。然后每小时的每个小时都有一个重叠的箭头指向当时的风向(北向0度,东向顺时针90度,等等)

1 个答案:

答案 0 :(得分:2)

您可以尝试使用matplotlib的annotate。与Arrow s和FancyArrow s相比,这通常不那么令人头疼:

import numpy as np
import matplotlib.pyplot as plt

time = np.linspace(0,23,24)
temp = np.array([10]*24) + np.random.random(24)*2
wind = np.linspace(0, 270, 24)

fig, ax = plt.subplots()
ax.plot(time, temp, 'o-')

arrow_len = 1.5
for i, theta in enumerate(np.radians(wind)):
    dx = arrow_len * np.cos(theta)
    dy = arrow_len * np.sin(theta) * ax.get_data_ratio()
    x, y = time[i], temp[i]
    ax.annotate("",
            xy=(x+dx, y+dy), xycoords='data',
            xytext=(x, y), textcoords='data',
            arrowprops=dict(arrowstyle="-|>")
            )
plt.show()

enter image description here