I am trying to create a horizontal barchart similar to the following image.
The small black vertical lines that appear on each bar, correspond to the number of āTotal Mentions Volumeā for a previous period. I have tried building it with plotly with the following code.
def create_lines(sentiment, mentions2):
"""Creates a line that corresponds to the number of mentions during the previous period"""
y = {'Negative': 1.7, 'Neutral':0.7, 'Positive':-0.3}
line = dict(type="line",
x0=mentions2,
y0=y[sentiment],
x1=mentions2,
y1=y[sentiment]+0.6,
line=dict(
color="#706e6e",
width=2,
# dash="dashdot",
))
return line
def create_barchart():
sentiments = ['Positive','Neutral', 'Negative']
# number of mentions this period
mentions1 = [120,80, 10]
# number of mentions the previous period
mentions2 = [80, 90, 15]
#create a list of shapes by calling the function create_shapes() for each sentiment
shapes = []
i = 0
for sentiment in sentiments:
shapes.append(create_lines(sentiment, mentions2[i]))
i += 1
layout = dict(
showlegend=False,
height=130,
xaxis=dict(showgrid=True, visible=True, title='Total Mention Volume'),
yaxis=dict(showgrid=False),
#plot_bgcolor='white',
margin=dict(t=20,
b=30,
l=60,
r=20,
),
shapes= shapes
)
graph = dcc.Graph(
id='sentiment_bar_plot',
figure={
'data': [
go.Bar(x= mentions1,
y=sentiments,
orientation='h',
width=[0.5, 0.5, 0.5],
opacity=1,
marker_color=['#04b568', '#bfbdbd', '#fc0000'],
),
],
'layout': layout
},
config=config
)
return graph
The above code produces the following chart.
I also tried using a second set of bars and ābarmode= āoverlayāā but I couldnāt achieve the desired result. Is there a more simple way to build a similar barchart in plotly?