【问题标题】:Coloring slices of pie chart in matplotlib python在matplotlib python中着色饼图切片
【发布时间】:2013-05-19 21:31:49
【问题描述】:

假设,我有一个如下饼图;我想根据向量中的一些值为切片着色,作为蓝色到红色之间的渐变。蓝色代表-1,红色+1,没有大于1也没有小于-1的值。

fracs = [33,33,33]
starting_angle = 90
axis('equal')
patches, texts, autotexts = pie(fracs, labels = None, autopct='%1.1f%%', startangle=90)

print patches[2]

for item in autotexts:
    item.set_text("")

subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.0, hspace=-0.4)


savefig('/home/superiois/Downloads/projectx3/GRAIL/pie2')
show()

【问题讨论】:

标签: matplotlib visualization pie-chart


【解决方案1】:
fracs = [33,33,33]
starting_angle = 90
axis('equal')

color_vals = [-1, 0, 1]

my_norm = matplotlib.colors.Normalize(-1, 1) # maps your data to the range [0, 1]
my_cmap = matplotlib.cm.get_cmap('jet') # can pick your color map

patches, texts, autotexts = pie(fracs, labels = None, autopct='%1.1f%%', startangle=90,
                colors=my_cmap(my_norm(color_vals)))

print patches[2]

for item in autotexts:
    item.set_text("")


subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.0, hspace=-0.4)

show()

您可以使用colors kwarg 设置补丁的颜色。您可以利用现有的颜色映射代码(例如在imshow 中使用)来生成所需的渐变。 Normalize 对象负责将您的数据标准化为颜色映射期望作为输入的范围,并且颜色映射将浮点数转换为颜色。

Normalize doc

pie doc

ColorMap doc

【讨论】:

  • 问题是在每个切片上放置渐变。我希望看到“蓝色、白色和红色”的幻灯片。
  • @user702846 这不是你问的问题,你应该澄清你的OP。
猜你喜欢
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多