使用matplotlib构建一些绘图时,我无法解决这个(看起来很简单)问题。 有
有代表范围内整数的灰度颜色。 0-255-更高的数字意味着更暗。 在本例中,可以简化df:colors=PD.data frame { ' color1' : [ 15 ]、' color2': [27]、' color2': [27]和'
' color4': [123],' color5': [220],' color6': [100],
' color7': [123],' color8': [247],' color9': [255]} )
现在,我想通过循环,用以下颜色更改绘制的背景。
^{pr2}$
通过使用matplotlib文档,在matplotlibrecognizesthefollowingformatstospecifyacolor : anrgborrgbatupleoffloatvaluesin [
或(0.1、0.2、0.5、0.3 );
ahexRGBorRGBAstring(e.g .' #0F0F0F' or
' #0F0F0F0F ';
astringrepresentationofafloatvaluein [ 0,1 ]
inclusiveforgraylevel(e.g .' 0.5 '; one of {'b '、' g '、' r '、' c ',
' m '、' y '、' k '、' w'};
a X11/CSS4 color name;
a name from the xkcd color
survey; 首选with ' xkcd : ' (e.g .' xkcd:sky blue ' );
one of
{'tab:blue '、' tab:orange '、' tab:green '、' tab:red '、' tab:purple ',
' tab:brown ',' tab:pink ',' tab:gray ',' tab:olive ',' tab:cyan'} which
arethetableaucolorsfromthe‘T10’categorical palette (which is
the default color cycle;
a“cn”color spec,i.e. 'C' followed by a
single digit,whichisanindexintothedefaultpropertycycle
(matplotlib.rcparams [ ' axes.prop _ cycle ' ]; the indexing occurs at
artistcreationtimeanddefaultstoblackifthecycledoesnot
包含颜色。
使用这些SO回答:
转换为改写代码: defRGB_int2tuple(RGBint ) :
返回(RGB int//256//256 % 6,rgbint //256 % 256,rgbint % 256 ) )
colors=PD.data frame ({ ' color1' : [ 15 ],' color2': [27],' color3': [89],' color4' : [ 115 )
' color5': [220],' color6': [100],' color7': [123],' color8': [247],' color9':[
fig,ax=PLT.subplots (3,3,fig size=(10,10 ),sharey='row ',sharex='col ' )
fig.subplots_adjust(Hspace=0,wspace=0) ) ) ) ) ) ) ) )。
column=0
forIinrange(3) :
forjinrange(3) :
color=colors.iloc[0,column]
color=255 - color
蓝色,绿色,red=RGB_int2tuple(color ) )。
print(f ) I ),{j}: ) color(t ) blue (green ) red ) )
ax[i,j ].set _ face color ((蓝/255,绿色/255,红色/255 ) )
column =1
但是结果是:
这是我进入第一步,怎么让python知道我的0-255刻度是灰色的? 有
编辑:
我又读了一遍matplotlib.colors文件,发现了astringrepresentationofafloatvaluein [ 0,1 ]
inclusiveforgraylevel(e.g .' 0.5 ';
使用这个:
重写我的代码: colors=PD.data frame ((color1' : [ 15 ],color2': [27],color3': [89],color4'3360 )
' color5': [220],' color6': [100],' color7': [123],' color8': [247],' color9':[
fig,ax=PLT.subplots (3,3,fig size=(10,10 ),sharey='row ',sharex='col ' )
fig.subplots_adjust(Hspace=0,wspace=0) ) ) ) ) ) ) ) )。
column=0
forIinrange(3) :
forjinrange(3) :
color=colors.iloc[0,column]
color=255 - color
color=color/255
ax[i,j].set_facecolor(str ) color ) )
column =1
这是给我的:
但是,我认为这是最好的解决办法。 有