线宽被添加到一行的长度 [英] Linewidth is added to the length of a line

查看:43
本文介绍了线宽被添加到一行的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在matplotlib中绘制线段时,线宽似乎被添加到了线的长度上.在我的代码下面(不是最 Pythonic 的代码,但它应该可以解决问题).我是在做错什么还是这只是matplotlib的功能?

When I draw a line segment in matplotlib the linewidth seems to be added to the length of the line. Below my code (not the most pythonic code, but it should do the trick). Am I doing something wrong or is this just a feature of matplotlib?

import matplotlib.pyplot as plt
import numpy as np

L1 = 100
L2 = 75
L3 = 100
Y = 3
N = 5
l_prev = 0
for l, c in zip(np.linspace(0, L1, N), range(N)):
    plt.plot([l_prev, l], [0, 0], 'r', linewidth=20)
    l_prev = l
l_prev = L1
for l, c in zip(np.linspace(L1, L1 + L2, N), range(N)):
    plt.plot([l_prev, l], [Y, Y], 'g', linewidth=1)
    l_prev = l
l_prev = L1
for l, c in zip(np.linspace(L1, L1 + L3, N), range(N)):
    p = plt.plot([l_prev, l], [-Y, -Y], 'b', linewidth=10)
    l_prev = l
plt.axvspan(xmin=L1, xmax=L1)
plt.axis([-5, 205, -5, 5])
plt.show()

我期望看到的是三个线段:[0,L1]、[L1,L2] 和 [L1,L3].但是第一行 [0,L1] 扩展到 L1 + 'thediameter'....

What I expected to see is three line segments: [0,L1], [L1,L2] and [L1,L3]. But the first line [0,L1] extends to L1 + 'the diameter'....

推荐答案

看起来默认的 solid_capstyle projecting ,这不是您想要的:

It looks like the default solid_capstyle is projecting, which isn't the one you want:

plt.figure()
plt.plot([0, 100], [5, 5], linewidth=50, linestyle="-", c="blue",
         solid_capstyle="butt")
plt.plot([0, 100], [15, 15], linewidth=50, linestyle="-", c="red",
         solid_capstyle="round")
plt.plot([0, 100], [25, 25], linewidth=50, linestyle="-", c="purple",
         solid_capstyle="projecting")
plt.axvline(x=100, c="black")
plt.xlim(0, 125)
plt.ylim(0, 30)
plt.savefig("cap.png")

这篇关于线宽被添加到一行的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆