【DS with Python】Matplotlib入门(三):cm模块、colormap配色、animation动画与canvas交互设计


一、Matplotlib配色

1.1 预定义的配色

  在matplotlib中默认了很多颜色,比如’k‘代表黑色,’r‘代表红色,这些都是最简单的配色,其他还有例如’azure’,’cyan'等也是我常用到的浅色预定义配色,更多预定义配色可以在官网查看:

  链接:matplotlib预定义配色

  如果认为颜色过深,还可以用aplha参数来调整,取值范围为0-1。

  这里我们用ax.add_patch()来做两个交叠的矩形来展示,其中zorder表示各个图层的位置,数值大的在数值小的上层:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle


fig,axes=plt.subplots(1,2,figsize=(8,4))


ax=axes[0]
ax.add_patch(Rectangle(xy=(0.5,0.5),width=.3,height=.3,fc='tomato',zorder=1,label='zorder=1'))
ax.add_patch(Rectangle(xy=(0.3,0.3),width=.3,height=.3,fc='royalblue',zorder=2,label='zorder=2'))
ax.set_title('alpha=1,blue over red')
ax.legend(loc='best')

ax=axes[1]
ax.add_patch(Rectangle(xy=(0.5,0.5),width=.3,height=.3,fc='tomato',zorder=2,label='zorder=2',alpha=0.3))
ax.add_patch(Rectangle(xy=(0.3,0.3),width=.3,height=.3,fc='royalblue',zorder=1,label='zorder=1',alpha=0.3))
ax.set_title('alpha=0.3,red over blue')
ax.legend(loc='best')

在这里插入图片描述
也可以直接在matplotlib.colors.cnames查看所有名字和对应的16进制码

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
import math
%matplotlib notebook

names={
   }
import matplotlib
for name, hex in matplotlib.colors.cnames.items():
    names.update({
   name:hex})

fig 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值