Я создал почти все, что вы хотите.К сожалению, я не нашел способа правильно установить заголовок в легенде (annotations
не подходит для установки заголовка легенды).А для отображения чисел (1.0,2.0) необходимо создать дополнительный столбец со значениями (столбец - df["Severity numbers"]
).
Код:
# import all the necessaries libraries
import pandas as pd
import plotly
import plotly.graph_objs as go
# Create DataFrame
df = pd.DataFrame({"Name":["User1","User1", "User1","User2"],
"Defect severity":["Medium","Medium","High","High"],
"Severity numbers":[1,1,2,2]})
# Create two additional DataFrames to traces
df1 = df[df["Defect severity"] == "Medium"]
df2 = df[df["Defect severity"] == "High"]
# Create two traces, first "Medium" and second "High"
trace1 = go.Bar(x=df1["Name"], y=df1["Severity numbers"], name="Medium")
trace2 = go.Bar(x=df2["Name"], y=df2["Severity numbers"], name="High")
# Fill out data with our traces
data = [trace1, trace2]
# Create layout and specify title, legend and so on
layout = go.Layout(title="Severity",
xaxis=dict(title="Name"),
yaxis=dict(title="Count of defect severity"),
legend=dict(x=1.0, y=0.5),
# Here annotations need to create legend title
annotations=[
dict(
x=1.05,
y=0.55,
xref="paper",
yref="paper",
text=" Defect severity",
showarrow=False
)],
barmode="group")
# Create figure with all prepared data for plot
fig = go.Figure(data=data, layout=layout)
# Create a plot in your Python script directory with name "bar-chart.html"
plotly.offline.plot(fig, filename="bar-chart.html")
Вывод: