简体   繁体   中英

Line plot with superimposed arrows in matplotlib

I'm trying to plot temperature and windspeed throughout the day as a function of time. What I was hoping to do was to plot temperature as a normal line graph. Then every hour on the hour have a superimposed arrow pointing in the direction of the wind at that moment (0 degrees for north, 90 degrees clockwise for east, etc...)

You could try using matplotlib's annotate . This is usually less of a headache than Arrow s and 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM