R v*_*ren 5 python plot matplotlib line
如果在 Matplotlib 中设置线宽,则必须以磅为单位给出线宽。就我而言,我有两个圆,半径均为 R,我想用一条线将它们连接起来。我希望这条线是 2*R 宽以获得棒状。但是当我说myLines[i].set_linewidth(2*R)这会使线条始终具有特定的粗细,无论我放大了多少。
有没有办法使线条具有特定的粗细,而不是基于像素或点的数量,而是随轴缩放?如何使我的线条与圆圈的直径具有相同的宽度?
我希望我能很好地解释自己,我期待着答案。
为了用数据单位的线宽绘制一条线,您可能需要看看这个答案。
data_linewidth_plot它使用一个与命令签名非常相似的类plt.plot()。
l = data_linewidth_plot( x, y, ax=ax, label='some line', linewidth = 1, alpha = 0.4)
Run Code Online (Sandbox Code Playgroud)
线宽参数以 (y-) 数据单位解释。
使用这一解决方案甚至不需要画圆圈,因为人们可以简单地使用solid_capstyle="round"参数。
R=0.5
l = data_linewidth_plot( [0,3], [0.7,1.4], ax=ax, solid_capstyle="round",
linewidth = 2*R, alpha = 0.4)
Run Code Online (Sandbox Code Playgroud)
正如您已经发现的,线宽是在轴空间中指定的,而不是在数据空间中指定的。要在数据空间中绘制一条线,请绘制一个矩形:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle
r = 5 # rod radius
x1, y1 = (0,0) # left end of rod
x2, y2 = (10,0) # right end of rod
# create 2 circles and a joining rectangle
c1 = Circle((x1, y1), r, color='r')
c2 = Circle((x2, y2), r)
rect = Rectangle((x1, y1-r), width=x2-x1, height=2*r)
# plot artists
fig, ax = plt.subplots(1,1)
for artist in [c2, rect, c1]:
ax.add_artist(artist)
# need to set axis limits manually
ax.set_xlim(x1-r-1, x2+r+1)
ax.set_ylim(y1-r-1, y2+r+1)
# set aspect so circle don't become oval
ax.set_aspect('equal')
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2183 次 |
| 最近记录: |