英文:
Wants to create horizontal bar chart with customized text annotations with various positions
问题
我想创建一个如下所示的水平条形图
我该如何实现这个目标?
我正在使用以下代码进行工作:
x_data = [float(23.1), float(42.21)]
y_data = ['排放', '绿色能源']
fig = go.Figure()
fig.add_trace(go.Bar(
y=y_data,
x=x_data,
orientation='h',
marker=dict(
color='rgba(246, 78, 139, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=0)
),
width=.2
))
fig.add_trace(go.Bar(
y=y_data,
x=[100-x_data[0], 100-x_data[1]],
orientation='h',
marker=dict(
color='rgba(58, 71, 80, 0.6)',
line=dict(color='rgba(58, 71, 80, 1.0)', width=0)
),
width=.2
))
fig.update_layout(paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)", barmode='stack')
annotations = []
title = ['排放', '绿色能源']
for i in range(0, 2):
annotations.append(
dict(x=x_data[i], y=y_data[i],
text=title[i],
font=dict(family='Arial', size=12,
color='#27235c'),
xshift=0,
yshift=20,
showarrow=True))
# fig.update_traces(texttemplate='%{y}', textposition='auto')
fig.update_yaxes(visible=False, showticklabels=False)
fig.update_layout(xaxis_title=None)
fig.update_layout(
activeselection_fillcolor=settings['graph_design_theme'][0])
fig.update_layout(autosize=False, width=310, height=310)
fig.update_layout(margin=dict(t=50, b=50, l=50, r=50))
fig.update_layout(annotations=annotations)
fig.update_layout(showlegend=False)
我需要复制图像中的效果,建议我选择哪种图表类型以及如何实现?
英文:
I want to create a horizontal bar chart as given below
How can I achieve this,
I am working this with following code
x_data = [float(23.1), float(42.21)]
y_data = ['Emission', 'Green Energy']
fig = go.Figure()
fig.add_trace(go.Bar(
y=y_data,
x=x_data,
orientation='h',
marker=dict(
color='rgba(246, 78, 139, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=0)
),
width=.2
))
fig.add_trace(go.Bar(
y=y_data,
x=[100-x_data[0], 100-x_data[1]],
orientation='h',
marker=dict(
color='rgba(58, 71, 80, 0.6)',
line=dict(color='rgba(58, 71, 80, 1.0)', width=0)
),
width=.2
))
fig.update_layout(paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)", barmode='stack')
annotations = []
title = ['Emission', 'Green Energy Generated']
for i in range(0, 2):
annotations.append(
dict( x=x_data[i], y=y_data[i],
text=title[i],
font=dict(family='Arial', size=12,
color='#27235c'),
xshift=0,
yshift=20,
showarrow=True))
# fig.update_traces(texttemplate='%{y}', textposition='auto')
fig.update_yaxes(visible=False, showticklabels=False)
fig.update_layout(xaxis_title=None)
fig.update_layout(
activeselection_fillcolor=settings['graph_design_theme'][0])
fig.update_layout(autosize=False, width=310, height=310)
fig.update_layout(margin=dict(t=50, b=50, l=50, r=50))
fig.update_layout(annotations=annotations)
fig.update_layout(showlegend=False)
I need to replicate as such in that image, suggest me to pick a graph and how can I achieve this?
答案1
得分: 1
以下是翻译好的部分:
有许多创建所需图表的方法,但我使用了子图来放置每个图表,隐藏了x和y轴,并注释了文本。我没有创建所有的注释,但我已经添加了代码,允许您扩展您的注释。
这种类型的图表不可避免地需要手动配置,因此表达您的偏好涉及大量的试验和错误。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
x_data = [float(23.1), float(42.21)]
y_data = ['Emission', 'Green Energy']
# fig = go.Figure()
fig = make_subplots(rows=2, cols=1, subplot_titles=y_data, shared_xaxes=True, vertical_spacing=0.00)
fig.add_trace(go.Bar(
y=[y_data[0]],
x=[x_data[0]],
orientation='h',
marker=dict(
color='rgba(58, 71, 80, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=0)
),
width=.2,
showlegend=False
), row=1, col=1)
# 其余部分翻译同上...
请注意,这只是您提供的代码的一部分。如果需要完整的翻译,请提供更多的上下文。
英文:
There are many ways to create the desired graphs, but I have used subplots to place each graph, hide the x- and y-axes and annotate the text. I have not created all the annotations, but I have added code to allow you to extend yours.
This type of graph inevitably requires manual configuration, so expressing your preferences involves a lot of trial and error.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
x_data = [float(23.1), float(42.21)]
y_data = ['Emission', 'Green Energy']
# fig = go.Figure()
fig = make_subplots(rows=2, cols=1, subplot_titles=y_data, shared_xaxes=True, vertical_spacing=0.00)
fig.add_trace(go.Bar(
y=[y_data[0]],
x=[x_data[0]],
orientation='h',
marker=dict(
color='rgba(58, 71, 80, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=0)
),
width=.2,
showlegend=False
), row=1, col=1)
fig.add_trace(go.Bar(
y=[y_data[0]],
x=[100-x_data[0]],
orientation='h',
marker=dict(
color='rgba(246, 78, 139, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=0)
),
width=.2,
showlegend=False
), row=1, col=1)
fig.add_trace(go.Bar(
y=[y_data[1]],
x=[x_data[1]],
orientation='h',
marker=dict(
color='rgba(58, 71, 80, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=0)
),
width=.2,
showlegend=False
), row=2, col=1)
fig.add_trace(go.Bar(
y=[y_data[1]],
x=[100-x_data[1]],
orientation='h',
marker=dict(
color='rgba(246, 78, 139, 0.6)',
line=dict(color='rgba(58, 71, 80, 1.0)', width=0)
),
width=.2,
showlegend=False
), row=2, col=1)
fig.add_annotation(x=2.0, y=0, yshift=-15, text=str(x_data[0]), showarrow=False, row=1, col=1)
fig.add_annotation(x=99, y=0, yshift=-15, text=str(100), showarrow=False, row=1, col=1)
fig.add_annotation(x=99, y=.4, yshift=-15, text='goal', showarrow=False, row=1, col=1)
fig.add_annotation(x=2.0, y=0, yshift=-15, text=str(x_data[1]), showarrow=False, row=2, col=1)
fig.add_annotation(x=99, y=0, yshift=-15, text=str(100), showarrow=False, row=2, col=1)
fig.update_layout(title='CARBON FOOTPRINT', title_font=dict(family='Arial', size=18, color='white'))
for i in range(0, 2):
fig['layout']['annotations'][i]['x'] = 0.0
fig['layout']['annotations'][i]['xanchor'] = 'left'
if i == 0:
fig['layout']['annotations'][i]['y'] = 0.9
elif i ==1:
fig['layout']['annotations'][i]['y'] = 0.4
fig.update_layout(barmode='stack', template='plotly_dark')
fig.update_layout(autosize=False, width=310, height=310, margin=dict(t=50, b=50, l=50, r=50))
fig.update_xaxes(showticklabels=False, showgrid=False)
fig.update_yaxes(showticklabels=False, showgrid=False)
fig.update_xaxes(zeroline=True, zerolinewidth=1, zerolinecolor='black')
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论