Python&Plotly:通过字典自定义饼图颜色
我正在创建报告引擎,我正在努力按照自己的意愿完成定制工作。我有包含1-10级临界值的数据,我希望在饼图中,10级总是变成红色,9级变成橙色等等 我目前的代码如下:Python&Plotly:通过字典自定义饼图颜色,python,colors,plotly,Python,Colors,Plotly,我正在创建报告引擎,我正在努力按照自己的意愿完成定制工作。我有包含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 p
#!/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')
但结果如下图所示:
有没有一种方法可以根据以下命令映射这些颜色:
colors = {
'10':'#ff0000'
'9':'#ff5050'
etc.. }
附加问题:有没有办法从10->1对右侧的图标进行排序
让我们假设您有自己的颜色。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)]
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]
现在只需使用标记:{'colors':colors}来绘制颜色
这将为您提供具有正确颜色的绘图
奖金问题
默认情况下,饼图按值排序,但是,您可以使用标签而不是使用sort:False进行排序
不幸的是,这不符合您构建图表的方式,因为它将按照数据的顺序进行:即1、10、2、6等,而这不是您想要的
最好按如下方式绘出每个值的已聚合数字:
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)]
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)
这将产生以下结果,我相信这正是您想要的:
颜色词典
让我们假设您有自己的颜色。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)]
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]
现在只需使用标记:{'colors':colors}来绘制颜色
这将为您提供具有正确颜色的绘图
奖金问题
默认情况下,饼图按值排序,但是,您可以使用标签而不是使用sort:False进行排序
不幸的是,这不符合您构建图表的方式,因为它将按照数据的顺序进行:即1、10、2、6等,而这不是您想要的
最好按如下方式绘出每个值的已聚合数字:
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)]
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)
这将产生以下结果,我相信这正是您想要的:
请替换df=pd.read_csv'example.csv',分隔符=';',error\u bad\u lines=False由一些示例数据复制粘贴到这里,以允许人们在他们这边运行您的示例。这将增加你快速得到正确答案的机会。谢谢。好的,我认为这与问题无关,但我已经做了:请替换df=pd.read_csv'example.csv',delimiter=';',error\u bad\u lines=False由一些示例数据复制粘贴到这里,以允许人们在他们这边运行您的示例。这将增加你快速得到正确答案的机会。谢谢。好吧,我认为这与问题无关,但我已经回答了:太棒了-正是我想要的+更多-谢谢!很高兴这有帮助。如果答案真的有用,那么请接受它和/或向上投票,以使处于相同情况的其他人更容易快速找到它。精彩-正是我想要的+更多-谢谢!很高兴这有帮助。如果答案确实有用,那么请接受它和/或向上投票,使处于相同情况的其他人更容易快速找到答案。