Plotly horizontal bar plots giving unexpected results

Viewed 656

I'm trying to create a horizontal bar plot similar to this one.

I can generate a vertical plot, but when I change it to horizontal the plot is a mess. I'm trying this two ways, using go and px. I don't understand the problem and am not sure how to proceed. Here is my code:

Using go:

import pandas as pd
import chart_studio.plotly as py
import plotly.graph_objects as go

dfs = pd.read_html('https://coronavirus.jhu.edu/data/mortality', header=0)
df = dfs[0]

df['Case-Fatality'] = list(map(lambda x: x[:-1], df['Case-Fatality'].values))

df['Case-Fatality'] = pd.to_numeric(df['Case-Fatality'], downcast='float')
df_sort = df.sort_values(by='Case-Fatality')
fig = go.Figure(go.Bar(
    x=df_sort['Country'],
    y=df_sort['Case-Fatality'],
    ))
fig.update_layout(
    title='',
    xaxis_title='',
    yaxis_title='')
fig.show()

Using px:

import plotly.express as px
fig = px.bar(df_sort, x='Country', y = 'Case-Fatality', orientation='h')
fig.show()

Following the plotly documentation, I should be able to add orientation='h' argument to either go.bar or px.bar. When I do, there is no error but the plot does not look correct.

Here is vertical, plotted properly but not the preferred orientation: enter image description here

Here is with the horizontal orientation, it looks the same with either go or px: enter image description here

Any help or guidance on why this is not working would be most appreciated.

1 Answers

Change x and y values around in either go.bar or px.bar (still add orientation = 'h').

Both methods work identically well.

Output:

enter image description here

Related