简体   繁体   English

Plotly 水平条形图给出了意想不到的结果

[英]Plotly horizontal bar plots giving unexpected results

I'm trying to create a horizontal bar plot similar to this one .我正在尝试创建一个与此类似的单杠 plot 。

I can generate a vertical plot, but when I change it to horizontal the plot is a mess.我可以生成一个垂直的 plot,但是当我将其更改为水平时,plot 是一团糟。 I'm trying this two ways, using go and px .我正在尝试这两种方法,使用gopx I don't understand the problem and am not sure how to proceed.我不明白这个问题,不知道如何继续。 Here is my code:这是我的代码:

Using go :使用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 :使用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 .plotly 文档之后,我应该能够将orientation='h'参数添加到go.barpx.bar When I do, there is no error but the plot does not look correct.当我这样做时,没有错误,但 plot 看起来不正确。

Here is vertical, plotted properly but not the preferred orientation:这是垂直的,正确绘制但不是首选方向: 在此处输入图像描述

Here is with the horizontal orientation, it looks the same with either go or px :这是水平方向,它看起来与gopx相同: 在此处输入图像描述

Any help or guidance on why this is not working would be most appreciated.任何关于为什么这不起作用的帮助或指导将不胜感激。

Change x and y values around in either go.bar or px.bar (still add orientation = 'h' ).go.barpx.bar中更改 x 和 y 值(仍然添加orientation = 'h' )。

Both methods work identically well.两种方法的效果都一样好。

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM