使用 plotly 根据分组条形图中的 RGB 值更改颜色
Changing colour based on RGB values in a grouped bar plot with plotly
我写了这段代码来生成分组条形图:
import plotly.graph_objects as go
class_list = ['class1','class2','class3','class4','class5']
average_length = [10,20,50,30,50]
num_entries = [83,38,83,57,34]
col_Type = ["A", "A", "A", "B", "B", "B"]
fig = go.Figure(data=[
go.Bar(name='Average Cars Per Area', x=class_list, y=average_length), #color=col_Type,
go.Bar(name='Cars Per Length', x=class_list, y=num_entries), #color = col_Type
])
# Change the bar mode
fig.update_layout(barmode='group')
#colours = {
# 'class_list': "#0C3B5D",
# 'num_entries': "#3EC1CD",
# 'average_length': "#EF3A4C"
#}
fig.update_layout(title_text='Average Cars Per Area and Length',
title_x=0.1,
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
bargap=0.30,
bargroupgap=0.0,
margin=dict(l=50, r=50, t=50, b=50),
xaxis_title="Score Class",
yaxis_title="Gene Length/Number of Proteins",
yaxis = dict(
tickfont = dict(size=13)),
# color_discrete_map = colours,
xaxis = dict(
tickfont = dict(size=13)),)
#fig.update_traces(marker_color=['red', 'green'], showlegend=False)
#fig.update_traces(marker_color=['rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)'], showlegend=False)
fig.update_xaxes(showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(showline=True, linewidth=2, linecolor='black')
fig.show()
输出为:
我想根据指定的 RGB 值更改条形的颜色。我可以在不同的地方看到关于如何做到这一点的答案(例如 here, here, )。但是正如您从我已注释掉的尝试中看到的那样,我正在努力让他们的解决方案为我工作。
有人可以演示如何根据 RGB 值更改条形颜色吗?
编辑 1:
在这里留下一个解决方案,以防它帮助任何人。这是使用 matplotlib 的一种方法。
# libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# set width of bars
barWidth = 0.25
# set heights of bars
bars1 = [10,20,50,30,50]
bars2 = [83,38,83,57,34]
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
# Make the plot
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['class1','class2','class3','class4','class5'])
# Create legend & Show graphic
plt.legend()
plt.show()
我没有添加它作为答案,因为原来的标题是'with plotly',这没有解决。
条形图的颜色可以设置为标记颜色中的任意颜色。
fig = go.Figure(data=[
go.Bar(name='Average Cars Per Area', x=class_list, y=average_length, marker_color='red'),
go.Bar(name='Cars Per Length', x=class_list, y=num_entries, marker_color='green')
])
我写了这段代码来生成分组条形图:
import plotly.graph_objects as go
class_list = ['class1','class2','class3','class4','class5']
average_length = [10,20,50,30,50]
num_entries = [83,38,83,57,34]
col_Type = ["A", "A", "A", "B", "B", "B"]
fig = go.Figure(data=[
go.Bar(name='Average Cars Per Area', x=class_list, y=average_length), #color=col_Type,
go.Bar(name='Cars Per Length', x=class_list, y=num_entries), #color = col_Type
])
# Change the bar mode
fig.update_layout(barmode='group')
#colours = {
# 'class_list': "#0C3B5D",
# 'num_entries': "#3EC1CD",
# 'average_length': "#EF3A4C"
#}
fig.update_layout(title_text='Average Cars Per Area and Length',
title_x=0.1,
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
bargap=0.30,
bargroupgap=0.0,
margin=dict(l=50, r=50, t=50, b=50),
xaxis_title="Score Class",
yaxis_title="Gene Length/Number of Proteins",
yaxis = dict(
tickfont = dict(size=13)),
# color_discrete_map = colours,
xaxis = dict(
tickfont = dict(size=13)),)
#fig.update_traces(marker_color=['red', 'green'], showlegend=False)
#fig.update_traces(marker_color=['rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)',
# 'rgba(135, 206, 250, 0.5)',
# 'rgba(400, 206, 250, 0.5)'], showlegend=False)
fig.update_xaxes(showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(showline=True, linewidth=2, linecolor='black')
fig.show()
输出为:
我想根据指定的 RGB 值更改条形的颜色。我可以在不同的地方看到关于如何做到这一点的答案(例如 here, here,
有人可以演示如何根据 RGB 值更改条形颜色吗?
编辑 1: 在这里留下一个解决方案,以防它帮助任何人。这是使用 matplotlib 的一种方法。
# libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# set width of bars
barWidth = 0.25
# set heights of bars
bars1 = [10,20,50,30,50]
bars2 = [83,38,83,57,34]
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
# Make the plot
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['class1','class2','class3','class4','class5'])
# Create legend & Show graphic
plt.legend()
plt.show()
我没有添加它作为答案,因为原来的标题是'with plotly',这没有解决。
条形图的颜色可以设置为标记颜色中的任意颜色。
fig = go.Figure(data=[
go.Bar(name='Average Cars Per Area', x=class_list, y=average_length, marker_color='red'),
go.Bar(name='Cars Per Length', x=class_list, y=num_entries, marker_color='green')
])