如何绘制颜色列表饼图

发布于 2024-10-19 17:12:18 字数 214 浏览 6 评论 0原文

你好,我有一个字典,其中 3-int-tuple 表示颜色(作为键),还有一个 int 表示图像中该颜色出现的次数(作为值),

例如,这是一个具有 3 种颜色的 4x4 像素图像: {(87, 82, 44): 1, (255, 245, 241): 11, (24, 13, 9): 4}

我想绘制列表 [1,11,4] 的饼图,其中每个饼图的切片涂上了正确的颜色..我该怎么办?

hi i have a dict with 3-int-tuple representing color (as key) and an int representing the numbers of occurences of that color in an image (as value)

for exemple, this is a 4x4 pixels image with 3 colors:
{(87, 82, 44): 1, (255, 245, 241): 11, (24, 13, 9): 4}

i want to plot a pie chart of list [1,11,4] in which each slice of the piechart is colored with the right color.. how can i do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热鲨 2024-10-26 17:12:18

更新:其他答案来自Paul的要好得多,但我只是编辑原来的答案,直到它基本上相同:)(我无法删除这个答案,因为它已被接受。)实际上没有任何意义

。你想要吗?我刚刚采用了 matplotlib 文档中的示例 并将数据转换为参数pie() 期望:

# This is a trivial modification of the example here:
# http://matplotlib.sourceforge.net/examples/pylab_examples/pie_demo.html

from pylab import *

data = {(87, 82, 44): 1, (255, 245, 241): 11, (24, 13, 9): 4}

colors = []
counts = []

for color, count in data.items():
    colors.append([float(x)/255 for x in color])
    counts.append(count)

figure(1, figsize=(6,6))

pie(counts, colors=colors, autopct='%1.1f%%', shadow=True)
title('Example Pie Chart', bbox={'facecolor':'0.8', 'pad':5})

show()

结果如下所示:

结果饼图

Update: the other answer from Paul is much better but there's not really any point in me just editing my original answer until it's essentially the same :) (I can't delete this answer because it's accepted.)

Does this do what you want? I just took an example from the matplotlib documentation and turned your data into parameters that pie() expects:

# This is a trivial modification of the example here:
# http://matplotlib.sourceforge.net/examples/pylab_examples/pie_demo.html

from pylab import *

data = {(87, 82, 44): 1, (255, 245, 241): 11, (24, 13, 9): 4}

colors = []
counts = []

for color, count in data.items():
    colors.append([float(x)/255 for x in color])
    counts.append(count)

figure(1, figsize=(6,6))

pie(counts, colors=colors, autopct='%1.1f%%', shadow=True)
title('Example Pie Chart', bbox={'facecolor':'0.8', 'pad':5})

show()

The result looks like this:

The resulting pie chart

音栖息无 2024-10-26 17:12:18

马克领先我 5 分钟,所以分数应该归于他,但无论如何,这是我的答案(几乎相同,但更简洁):

from matplotlib import pyplot

data = {(87, 82, 44): 1, (255, 245, 241): 11, (24, 13, 9): 4}
colors, values = data.keys(), data.values()
# matplotlib wants colors as 0.0-1.0 floats, not 0-255 ints
colors = [tuple(i/255. for i in c) for c in colors]
pyplot.pie(values, colors=colors)
pyplot.show()

Mark beat me by 5 minutes, so points should go to him, but here's my (nearly identical, but more terse) answer anyway:

from matplotlib import pyplot

data = {(87, 82, 44): 1, (255, 245, 241): 11, (24, 13, 9): 4}
colors, values = data.keys(), data.values()
# matplotlib wants colors as 0.0-1.0 floats, not 0-255 ints
colors = [tuple(i/255. for i in c) for c in colors]
pyplot.pie(values, colors=colors)
pyplot.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文