gpt4 book ai didi

Python 和 Plotly : custom colors to pie chart via dictionary

转载 作者:行者123 更新时间:2023-12-05 08:54:17 25 4
gpt4 key购买 nike

我正在创建报告引擎,并且正在努力按照我的意愿进行自定义工作。我有包含 1-10 临界值的数据,我想拥有它,以便在馅饼中,评级 10 始终变为红色,评级 9 始终变为橙色,依此类推。

我当前的代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF

import numpy as np
import pandas as pd

crit = [1, 10, 2, 6, 1, 2, 2, 3, 1, 4, 6, 6, 9, 10, 5, 8, 3, 8, 5, 4, 9, 2, 8, 7, 1, 1, 7, 3, 9, 9, 6, 6, 8, 9, 6, 7, 5, 9, 8, 4, 4, 5, 6, 2, 9, 9, 4, 6, 9, 9]

fig = {
"data": [
{
"values": crit,
"labels": crit,
"domain": {"x": [0, .48]},
"name": "Criticality",
"marker": {'colors': [
'#e6f2ff',
'#99ccff',
'#ccccff',
'#cc99ff',
'#ff99ff',
'#ff6699',
'#ff9966',
'#ff6600',
'#ff5050',
'#ff0000'
]
},
"textinfo":"percent+label",
"textfont": {'color': '#FFFFFF', 'size': 15},
"hole": .4,
"type": "pie"
} ],
"layout": {
"title":"Criticalities",
"annotations": [
{
"font": {
"size": 25,
"color": '#5A5A5A'
},
"showarrow": False,
"text": "2018",
"x": 0.20,
"y": 0.5
}
]
}
}



py.iplot(fig, filename='pie-custom-colors')

但结果如下图所示:

graph

有没有办法根据像这样的字典来映射这些颜色:

colors = { 
'10':'#ff0000'
'9':'#ff5050'
etc.. }

奖金问题:有没有办法从 10->1 对右侧的图标进行排序

最佳答案

颜色词典

假设您有自己的 color_dict。 Plotly 采用与标签和值的向量具有相同长度的颜色数组。所以,你必须自己构造这个数组,例如像这样:

import numpy as np
crit = np.array([1, 10, 2, 6, 1, 2, 2, 3, 1, 4, 6, 6, 9, 10, 5, 8, 3, 8,
5, 4, 9, 2, 8, 7, 1, 1, 7, 3, 9, 9, 6, 6, 8, 9, 6, 7, 5,
9, 8, 4, 4, 5, 6, 2, 9, 9, 4, 6, 9, 9])
color_dict = {'1':'#e6f2ff', '2':'#99ccff', '3':'#ccccff',
'4':'#cc99ff', '5':'#ff99ff', '6':'#ff6699',
'7':'#ff9966', '8':'#ff6600', '9':'#ff5050',
'10':'#ff0000'}
colors = np.array([''] * len(crit), dtype = object)
for i in np.unique(crit):
colors[np.where(crit == i)] = color_dict[str(i)]

现在绘制只需使用 "marker": {'colors': colors} 作为颜色。

这将为您提供具有正确颜色的绘图。

奖金问题

饼图默认按值排序,但是,您可以使用标签而不是使用 "sort":False 进行排序。

不幸的是,这不适用于您构建图表的方式,因为它将采用数据出现的顺序:即 1、10、2、6 等,这不是您想要的。

最好像这样为 Plotly 提供已经汇总的每个值的数字:

labels = np.unique(crit) #or simply = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
values = np.bincount(crit)[1:] #[5, 5, 3, 5, 4, 8, 3, 5, 10, 2]

现在,无需将颜色放入某些字典中,因为它们已经与您的数据具有相同的形状。

fig = {
"data": [
{
"values": values,
"labels": labels,
"domain": {"x": [0, .48]},
"name": "Criticality",
"sort": False,
"marker": {'colors': ['#e6f2ff', '#99ccff', '#ccccff',
'#cc99ff', '#ff99ff', '#ff6699',
'#ff9966', '#ff6600', '#ff5050',
'#ff0000']},
"textinfo":"percent+label",
"textfont": {'color': '#FFFFFF', 'size': 15},

"hole": .4,
"type": "pie"
} ],
"layout": {
"title":"Criticalities",
"annotations": [
{
"font": {
"size": 25,
"color": '#5A5A5A'
},
"showarrow": False,
"text": "2018",
"x": 0.20,
"y": 0.5
}
]
}
}

py.iplot(fig)

这给出了以下结果,我相信这就是您想要的:

enter image description here

关于Python 和 Plotly : custom colors to pie chart via dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com