python pyplot颜色,使用matplotlib / pyplot混合线条颜色绘制曲线

本文介绍如何在Python的matplotlib库中创建从一种颜色渐变到另一种颜色的曲线。通过使用LineCollection和自定义colorlist函数实现颜色过渡,或者通过简化后的colorlist2函数直接计算颜色通道的平方根差异来达到相同效果。
摘要由CSDN通过智能技术生成

I want to start the curve with one color and progressively blend into another color until the end. The following function in my MCVE works, but surely, there has to be a better way I haven't found out about, yet?!

import numpy as np

import matplotlib.pyplot as plt

def colorlist(color1, color2, num):

"""Generate list of num colors blending from color1 to color2"""

result = [np.array(color1), np.array(color2)]

while len(result) < num:

temp = [result[0]]

for i in range(len(result)-1):

temp.append(np.sqrt((result[i]**2+result[i+1]**2)/2))

temp.append(result[i+1])

result = temp

indices = np.linspace(0, len(result)-1, num).round().astype(int)

return [result[i] for i in indices]

x = np.linspace(0, 2*np.pi, 100)

y =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值