文章目录
一、Matplotlib配色
1.1 预定义的配色
在matplotlib中默认了很多颜色,比如’k
‘代表黑色,’r
‘代表红色,这些都是最简单的配色,其他还有例如’azure
’,’cyan
'等也是我常用到的浅色预定义配色,更多预定义配色可以在官网查看:
如果认为颜色过深,还可以用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