python怎么变各种颜色_python – 在matplotlib中变暗或变亮颜色

这是一个来自

my gist的函数,用于减轻我认为可以使用matplotlib已知的任何颜色格式的任何颜色.我认为设定金额> 1也可能变暗.

def lighten_color(color, amount=0.5):

"""

Lightens the given color by multiplying (1-luminosity) by the given amount.

Input can be matplotlib color string, hex string, or RGB tuple.

Examples:

>> lighten_color('g', 0.3)

>> lighten_color('#F034A3', 0.6)

>> lighten_color((.3,.55,.1), 0.5)

"""

import matplotlib.colors as mc

import colorsys

try:

c = mc.cnames[color]

except:

c = color

c = colorsys.rgb_to_hls(*mc.to_rgb(c))

return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2])

编辑:确实,它确实变暗和变亮:

import matplotlib.pyplot as plt

import numpy as np

xs = np.linspace(-1, 1, 100)

plt.plot(xs, 0 * xs, color='b', lw=3)

plt.plot(xs, xs**2, color=lighten_color('b', 0.4), lw=3)

plt.plot(xs, -xs**2, color=lighten_color('b', 1.6), lw=3)

编辑2:删除函数中不需要的numpy依赖项.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值