当重叠时,如何在颜色变深的地方绘制透明线? [英] How can I draw transparent lines where the color becomes stronger when they overlap?

查看:119
本文介绍了当重叠时,如何在颜色变深的地方绘制透明线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样在matplotlib中绘制一堆透明线时,您会得到不错的效果。当它们重叠时,它们会变暗。

When you draw a bunch of transparent lines in matplotlib like this, you get a nice effect; when they overlap they are a bit darker.

from pylab import *
for _ in xrange(1000) :
    plot(np.random.randn(2),np.random.randn(2),alpha=0.1,color='k')
show()

它看起来像这样:

但是,如果您绘制一条像这样与自身重叠的长线,则该线不会与自身相互作用。看起来像这样:

But if you draw one long line like this that overlaps with itself like this, the line doesn't "interact with itself." It looks like this:

我会就像画一条与自身重叠的曲线,因此重叠的次数越多,颜色就越深。如果使用循环将曲线分解并分别绘制每个线段,我将得到所需的东西,但是在线段相遇处也会出现难看且不可接受的伪像,从而使曲线看起来像点划线或虚线。

I would like to draw a single curve that overlaps with itself, so that the more it overlaps with itself, the darker it becomes. If I use a loop to break up the curve and draw each line segment separately, I get what I want, but I also get ugly and unacceptable artifacts where the line segments meet, making the curve looked like a dotted or dashed line.. Like this:

有什么不错的绘画方法曲线,这样当它与自身重叠时会变暗,但是您不会得到像刚刚描述的那样的人工产物?

Is there any nice way to draw a curve so that it becomes darker when it overlaps with itself, but you don't get artifacts like those just described?

推荐答案

使用循环将曲线分解并分别绘制每个线段时,可以尝试将 solid_capstyle 参数用于 plot 。默认值为投射 ,但是您可以尝试使用对接 看看是否有帮助。

When using a loop to break up the curve and draw each line segment separately, you can try to use the solid_capstyle argument to plot. The default is "projecting", but you could try using "butt" and see if it helps.

plt.plot(x,y, alpha=0.1, c="k", solid_capstyle="butt")

这可能会稍微降低效果。

This might reduce the effect a little.

import matplotlib.pyplot as plt
import numpy as np

def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
    return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)

x,y = squiggle_xy(2.5, 2, 1, 3)

fig, ax = plt.subplots(ncols=2, figsize=(6,3))
ax[0].set_title("solid_capstyle=\"projecting\"")
ax[1].set_title("solid_capstyle=\"butt\"")
for i in range(len(x)-1):
    print x[i:i+2]
    ax[0].plot(x[i:i+2], y[i:i+2], alpha=0.1, lw=10, solid_capstyle="projecting", c="b")
    ax[1].plot(x[i:i+2], y[i:i+2], alpha=0.1, lw=10, solid_capstyle="butt", c="b")

plt.show()

请参见此问题,以很好地解释 solid_capstyle

See this question for a good explanation of solid_capstyle.

这篇关于当重叠时,如何在颜色变深的地方绘制透明线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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