如何在情节中制作计数图 [英] how to make countplot in plotly

查看:50
本文介绍了如何在情节中制作计数图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是情节的新手.我正在尝试在 plotly 中创建一个计数图.我正在读取一个数据帧,这是我在数据帧中的列和值.

I am new to plotly. I am trying to create a countplot in plotly. I am reading a dataframe and here are my columns and values in the dataframe.

名称缺陷严重性

User1 媒体

User1 媒体

User1 高

User2 高

这是我希望显示最终图表的方式

Here's how I would like the final graph to be shown

谁能建议我如何在 Plotly 中编码?

Can anyone suggest me how to code in Plotly?

推荐答案

我创建了几乎所有你想要的东西.不幸的是,我没有找到正确设置图例标题的方法(annotations 不是设置图例标题的好参数).并且要显示数字 (1.0,2.0),需要创建一个带有值的附加列(列 - df["Severity numbers"]).

I created almost all what you want. Unfortunately, I did not find a way to set the title in the legend correctly(annotations is not good parameter to set a legend title). And to display numbers (1.0,2.0) it is necessary to create an additional column with values (column - 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")

输出:

这篇关于如何在情节中制作计数图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆