0

我有这样一个情节,并想在下面的右侧添加一个颜色条码(哪个颜色对应于哪个数字)。我看到了一些用于 imshow 而不是饼图的示例。

#!/usr/bin/env python
"""
http://matplotlib.sf.net/matplotlib.pylab.html#-pie for the docstring.
"""
from pylab import *

fracs = [33,33,33]
starting_angle = 90
axis('equal')

for item in range(9):
    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('RdBu') # can pick your color map

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

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


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.5)

savefig('/home/superiois/Downloads/projectx3/GRAIL/pie1.png')
show()

另外,如果您告诉我如何自定义颜色条码的大小和位置,那就太好了;谢谢。

4

1 回答 1

3

通常,图例更适合离散值,而颜色条更适合连续值。也就是说,由于 mpl 允许您从头开始创建颜色条,因此它是可能的。

import matplotlib.pyplot as plt
import matplotlib as mpl

fracs = [33,33,33]
starting_angle = 90

fig, axs = plt.subplots(3,3, figsize=(6,6))
fig.subplots_adjust(hspace=0.1,wspace=0.0)

axs = axs.ravel()

for n in range(9):
    color_vals = [-1, 0, 1]
    my_norm = mpl.colors.Normalize(-1, 1) # maps your data to the range [0, 1]
    my_cmap = mpl.cm.get_cmap('RdBu', len(color_vals)) # can pick your color map

    patches, texts, autotexts = axs[n].pie(fracs, labels = None, autopct='%1.1f%%', startangle=90, colors=my_cmap(my_norm(color_vals)))
    axs[n].set_aspect('equal')

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

ax_cb = fig.add_axes([.9,.25,.03,.5])
cb = mpl.colorbar.ColorbarBase(ax_cb, cmap=my_cmap, norm=my_norm, ticks=color_vals)

cb.set_label('Some label [-]')
cb.set_ticklabels(['One', 'Two', 'Three'])

我添加了自定义刻度标签只是为了展示它是如何工作的,要获得默认值,只需删除最后一行。

在此处输入图像描述

于 2013-05-21T10:19:06.090 回答