python绘制有箭头的直线_python-Polar Matplotlib图中的箭头

本文展示了如何在Python的极坐标图中使用matplotlib库绘制带有箭头的直线。通过调整箭头的宽度、颜色和透明度,可以创建不同效果的箭头。示例代码包括设置图形样式、创建极坐标轴以及添加两个不同角度的箭头。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

箭头尺寸太大,这是:

import matplotlib

import numpy as np

import matplotlib.pyplot as plt

print "matplotlib.__version__ = ", matplotlib.__version__

print "matplotlib.get_backend() = ", matplotlib.get_backend()

# radar green, solid grid lines

plt.rc('grid', color='#316931', linewidth=1, linestyle='-')

plt.rc('xtick', labelsize=15)

plt.rc('ytick', labelsize=15)

# force square figure and square axes looks better for polar, IMO

width, height = matplotlib.rcParams['figure.figsize']

size = min(width, height)

# make a square figure

fig = plt.figure(figsize=(size, size))

ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')

r = np.arange(0, 3.0, 0.01)

theta = 2*n

Python 中,可以使用 matplotlib绘制风向、风力、风速。以下是示例代码: ```python import matplotlib.pyplot as plt import numpy as np # 风向、风力、风速数据 wind_direction = np.array([0, 45, 90, 135, 180, 225, 270, 315]) wind_speed = np.array([1, 2, 3, 4, 5, 6, 7, 8]) wind_power = np.array([0.2, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) # 极坐标系下的角度 angles = np.deg2rad(90 - wind_direction) # 绘制风向 fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.set_theta_zero_location("N") ax.set_theta_direction(-1) ax.set_rlim(0, 8) ax.set_rticks([1, 2, 3, 4, 5, 6, 7, 8]) ax.set_rlabel_position(22.5) ax.set_thetagrids(wind_direction, labels=None) for angle, speed in zip(angles, wind_speed): x = [angle, angle+0.1] y = [0, speed] ax.plot(x, y, color='blue') ax.annotate("", xy=(angle+0.1, speed), xytext=(angle, 0), arrowprops=dict(arrowstyle="->", color='blue')) # 绘制风力 fig, ax = plt.subplots() ax.bar(wind_direction, wind_power, width=30, alpha=0.5) ax.set_xlim(-15, 375) ax.set_ylim(0, 6) ax.set_xticks(wind_direction) ax.set_xlabel('Wind Direction') ax.set_ylabel('Wind Power') # 绘制风速 fig, ax = plt.subplots() ax.plot(wind_direction, wind_speed, color='red') ax.fill_between(wind_direction, wind_speed, alpha=0.5, color='red') ax.set_xlim(-15, 375) ax.set_ylim(0, 8) ax.set_xticks(wind_direction) ax.set_xlabel('Wind Direction') ax.set_ylabel('Wind Speed') plt.show() ``` 运行上述代码,即可得到带箭头的风向、风力、风速。其中,风向使用极坐标系绘制,风力使用柱状绘制,风速使用折线绘制。 风向: ![风向](https://img-blog.csdnimg.cn/20220117194530313.png) 风力: ![风力](https://img-blog.csdnimg.cn/20220117194613771.png) 风速: ![风速](https://img-blog.csdnimg.cn/20220117194653540.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值