1

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.

Name Defect severity

User1 Medium

User1 Medium

User1 High

User2 High

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

img

Can anyone suggest me how to code in Plotly?

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
Prasanna
  • 19
  • 1
  • 1
  • 5

3 Answers3

4

You can do it with two lines of code, with the help of pandas groupby and plotly's barmode attribute.

Plotly's bar chart has a specific attribute to control how to show the bars, it's called barmode, quoting the API documentation:

barmode: str (default 'relative') One of 'group', 'overlay' or 'relative' In 'relative' mode, bars are stacked above zero for positive values and below zero for negative values. In 'overlay' mode, bars are drawn on top of one another. In 'group' mode, bars are placed beside each other.

See the bar chart documentation for examples.

Now, for your example:

# import needed libraries
import pandas as pd
import plotly.express as px

# some dummy dataset
df = pd.DataFrame(
    {
        "Name": ["User1", "User1", "User1", "User2"],
        "Defect severity": ["Medium", "Medium", "High", "High"],
    }
)

You need to group by both Name and Defect severity columns, and then use the count aggregating function (I recommend you take a look at this question)

df = df.groupby(by=["Name", "Defect severity"]).size().reset_index(name="counts")

The data now will look like the following:

Name Defect severity counts
0 User1 High 1
1 User1 Medium 2
2 User2 High 1

Finally, you can use plotly bar chart:

px.bar(data_frame=df, x="Name", y="counts", color="Defect severity", barmode="group")

The chart would be:

enter image description here

There you go! with only two lines of code, you got a nice grouped bar chart.

Reslan Tinawi
  • 448
  • 4
  • 12
2

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"]).

Code:

# 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")

Output: Hope it is what you want

Dmitriy Kisil
  • 2,858
  • 2
  • 16
  • 35
1
data = [
    go.Bar(
        y=coach_sectors['Sectors'].value_counts().to_dense().keys(),
        x=coach_sectors['Sectors'].value_counts(),
        orientation='h',
        text="d",
    )]
layout = go.Layout(
    height=500,
    title='Sector/ Area of Coaches - Combined',
    hovermode='closest',
    xaxis=dict(title='Votes', ticklen=5, zeroline=False, gridwidth=2, domain=[0.1, 1]),
    yaxis=dict(title='', ticklen=5, gridwidth=2),
    showlegend=False
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='Sector/ Area of Coaches - Combined')
Cyanamous
  • 13
  • 2