gpt4 book ai didi

python - 如何使用 plotly express 更改条形图中每个特定条形的颜色?

转载 作者:行者123 更新时间:2023-12-05 02:33:24 25 4
gpt4 key购买 nike

我正在使用 python 和 plotly 来为我正在使用的数据集中某些类别的平均评分设计条形图。我几乎得到了我想要的条形图,但是我想更改图中每个特定条形的颜色,但似乎找不到关于如何在线执行此操作的明确方法。

数据集

from pandas import Timestamp
pd.DataFrame({'id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5},

'overall_rating': {0: 5, 1: 4, 2: 5, 3: 5, 4: 4},
'user_name': {0: 'member1365952',
1: 'member465943',
2: 'member665924',
3: 'member865886',
4: 'member1065873'},
'date': {0: Timestamp('2022-02-03 00:00:00'),
1: Timestamp('2022-02-03 00:00:00'),
2: Timestamp('2022-02-02 00:00:00'),
3: Timestamp('2022-02-01 00:00:00'),
4: Timestamp('2022-02-01 00:00:00')},
'comments': {0: 'Great campus. Library is always helpful. Sport course has been brill despite Civid challenges.',
1: 'Average facilities and student Union. Great careers support.',
2: 'Brilliant university, very social place with great unions.',
3: 'Overall it was very good and the tables and chairs for discussion sessions worked very well',
4: 'Uni is nice and most of the staff are amazing. Facilities (particularly the library) could be better'},
'campus_facilities_rating': {0: 5, 1: 3, 2: 5, 3: 4, 4: 4},
'clubs_societies_rating': {0: 5, 1: 3, 2: 4, 3: 4, 4: 4},
'students_union_rating': {0: 4, 1: 3, 2: 5, 3: 5, 4: 5},
'careers_service_rating': {0: 5, 1: 5, 2: 5, 3: 5, 4: 3},
'wifi_rating': {0: 5, 1: 5, 2: 5, 3: 5, 4: 3}})

使用的代码

# Plot to find mean rating for different categories
fig = px.bar(df, y=[df.campus_facilities_rating.mean(), df.clubs_societies_rating.mean(),
df.students_union_rating.mean(), df.careers_service_rating.mean(), df.wifi_rating.mean()],
x=['Campus Facilities', 'Clubs & Societies', 'Students Union', 'Careers & Services', 'Wifi'],
labels={
"y": "Mean Rating (1-5)",
"x": "Category"},
title="Mean Rating For Different Student Categories")

fig.show()

更新的尝试

# Plot to find mean rating for different categories
fig = px.bar(df, y=[df.campus_facilities_rating.mean(), df.clubs_societies_rating.mean(),
df.students_union_rating.mean(), df.careers_service_rating.mean(), df.wifi_rating.mean()],
x=['Campus Facilities', 'Clubs & Societies', 'Students Union', 'Careers & Services', 'Wifi'],
labels={
"y": "Mean Rating (1-5)",
"x": "Category"},
title="Mean Rating For Different Student Categories At The University of Lincoln",
color_discrete_map = {
'Campus Facilities' : 'red',
'Clubs & Societies' : 'blue',
'Students Union' : 'pink',
'Careers & Services' : 'grey',
'Wifi' : 'orange'})

fig.update_layout(barmode = 'group')

fig.show()

输出只是将所有条形图显示为蓝色。

最佳答案

一般来说,如果您定义了一个类别,您可以在 px.bar() 中使用 color_discrete_map 来指定每个条形图的颜色 比如 color="medal" 是这样的:

color_discrete_map={'gold':'yellow', 'silver':'grey', 'bronze':'brown'}

plotly :

enter image description here

带有数据样本的通用方法的完整代码:

import plotly.express as px

long_df = px.data.medals_long()

fig = px.bar(long_df, x="nation", y="count", color="medal", title="color_discrete_map={'gold':'yellow', 'silver':'grey', 'bronze':'brown'}",
color_discrete_map={'gold':'yellow', 'silver':'grey', 'bronze':'brown'})

fig.update_layout(barmode = 'group')

fig.show()

OP提供数据样本后编辑

对于您的特定数据集和结构,您不能直接应用 color='category,因为不同的类别分布在多个列中,如下所示:

enter image description here

有一种方法可以使用 go.Figure()fig.add_traces() 来达到您的目标,但是因为您似乎对 px.bar( ),我们将坚持使用 plotly.express。简而言之,go.Figure() 不需要特别的数据争论来获得你想要的,但设置图形会有点困惑。对于 plotly.expresspx.bar,情况恰恰相反。一旦我们对您的数据集进行了一些更改,构建下图所需的只是以下代码段:

fig = px.bar(dfg, x = 'category', y = 'value',
color = 'category',
category_orders = {'category':['Campus Facilities','Clubs & Societies','Students Union','Careers & Services','Wifi']},
color_discrete_map = {'Campus Facilities' : 'red',
'Clubs & Societies' : 'blue',
'Students Union' : 'pink',
'Careers & Services' : 'grey',
'Wifi' : 'orange'})

enter image description here

包含所有数据整理步骤的完整代码:

from pandas import Timestamp
import plotly.express as px
import pandas as pd
df = pd.DataFrame({'id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5},

'overall_rating': {0: 5, 1: 4, 2: 5, 3: 5, 4: 4},
'user_name': {0: 'member1365952',
1: 'member465943',
2: 'member665924',
3: 'member865886',
4: 'member1065873'},
'date': {0: Timestamp('2022-02-03 00:00:00'),
1: Timestamp('2022-02-03 00:00:00'),
2: Timestamp('2022-02-02 00:00:00'),
3: Timestamp('2022-02-01 00:00:00'),
4: Timestamp('2022-02-01 00:00:00')},
'comments': {0: 'Great campus. Library is always helpful. Sport course has been brill despite Civid challenges.',
1: 'Average facilities and student Union. Great careers support.',
2: 'Brilliant university, very social place with great unions.',
3: 'Overall it was very good and the tables and chairs for discussion sessions worked very well',
4: 'Uni is nice and most of the staff are amazing. Facilities (particularly the library) could be better'},
'campus_facilities_rating': {0: 5, 1: 3, 2: 5, 3: 4, 4: 4},
'clubs_societies_rating': {0: 5, 1: 3, 2: 4, 3: 4, 4: 4},
'students_union_rating': {0: 4, 1: 3, 2: 5, 3: 5, 4: 5},
'careers_service_rating': {0: 5, 1: 5, 2: 5, 3: 5, 4: 3},
'wifi_rating': {0: 5, 1: 5, 2: 5, 3: 5, 4: 3}})

df.columns = ['id', 'overall_rating', 'user_name', 'date', 'comments', 'Campus Facilities',
'Clubs & Societies','Students Union','Careers & Services','Wifi']

dfm = pd.melt(df, id_vars=['id', 'overall_rating', 'user_name', 'date', 'comments'],
value_vars=list(df.columns[5:]),
var_name ='category')

dfg = dfm.groupby(['category']).mean().reset_index()

fig = px.bar(dfg, x = 'category', y = 'value', color = 'category',
category_orders = {'category':['Campus Facilities','Clubs & Societies','Students Union','Careers & Services','Wifi']},
color_discrete_map = {
'Campus Facilities' : 'red',
'Clubs & Societies' : 'blue',
'Students Union' : 'pink',
'Careers & Services' : 'grey',
'Wifi' : 'orange'})

fig.update_yaxes(title = 'Mean rating (1-5)')
fig.show()

附录:为什么选择dfmdfg

px.bar(color = 'variable') 将颜色分配给一系列的唯一出现或名为 'variable' 的 pandas 列。但是我们对您的数据框感兴趣的类别分布在多个列中。那又怎样

dfm = pd.melt(df, id_vars=['id', 'overall_rating', 'user_name', 'date', 'comments'],
value_vars=list(df.columns[5:]),
var_name ='category')

确实,就是取以下列:

enter image description here

并将它们堆叠到一个名为 variable 的列中,如下所示:

enter image description here

但这仍然是原始数据,您对此不感兴趣,而是对同一列中每个组的平均值感兴趣。这就是

dfm.groupby(['category']).mean().reset_index()

给我们:

enter image description here

看看pd.melt()df.groupby()了解更多详情。

关于python - 如何使用 plotly express 更改条形图中每个特定条形的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71000920/

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