将 matplotlib 的自动颜色更改为十六进制颜色自动 python3



>我使用 matplotlib 创建了一个饼图,我想将默认颜色更改为更柔和的颜色,例如十六进制 RGB 或 RGBA 字符串颜色。到目前为止,我有以下脚本:

colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
explode = ((0.05,)*(len(annotation_df.index)))
fig1, ax1 = plt.subplots()
ax1.pie(annotation_df['count'], labels=annotation_df['annotation'], autopct='%1.1f%%', startangle=90, pctdistance=0.85, explode=explode,colors=colors)  #colors=colors,
# draw circle
centre_circle = plt.Circle((0, 0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
# Equal aspect ratio ensures that pie is drawn as a circle
ax1.axis('equal')
plt.tight_layout()
plt.show()

问题是我需要自动设置颜色,并且我不想专门编写颜色,如上面的脚本中所述。

有人知道该怎么做吗?

您可以定义一个颜色循环仪来包含要使用的颜色。

import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = plt.cycler('color', 
['#ff9999', '#66b3ff', '#99ff99', '#ffcc99'])
fig1, ax1 = plt.subplots()
ax1.pie([1,2,3], labels=list("ABC"), autopct='%1.1f%%')
ax1.axis('equal')
plt.tight_layout()
plt.show()

如果楔块少于循环仪中的颜色,则仅使用所需的颜色。如果循环仪中的楔子多于颜色,则重复使用它们。您可以将任意数量的颜色放入颜色循环仪中。

最新更新