简体   繁体   English

使用plotly创建条形图

[英]create a bar chart using plotly

I'm trying to create a bar chart using plotly . 我正在尝试使用plotly创建条形图。

The input data looks like: 输入数据如下:

In [7]: datag.ix[:,0].tolist()[1:6]
Out[7]: [2020.0, 5000.0, 6010.0, 6017.0, 6020.0]

and

In [8]:datag.ix[:,1].tolist()[1:6]
Out[8]: 
[0.005178087393490427,
 0.0014053668695097226,
 0.3174139251746979,
 0.006049724003653125,
 0.24824287385322272]

and the code is 和代码是

import plotly
import plotly.offline as offline
import plotly.plotly as py 
import plotly.graph_objs as go

trace1 = go.Bar(
        x=[str(x) for x in datag.ix[:,0].tolist()[1:6]],#datag.ix[:,0].tolist()[1:6],
        y=datag.ix[:,1].tolist()[1:6],
        name='travel'
        )
data=[trace1]
layout= go.Layout(
        barmode= 'stack',
        title='Realization: 0, 0',
        xaxis=dict(title='Model'),
        yaxis=dict(title='Time (minutes)')
        )
fig= go.Figure(data=data, layout=layout)
offline.plot(fig, image='png', filename='stacked-bar')

I get the following output: 我得到以下输出: 在此处输入图片说明 However, the problem is I want to demonstrate the x-data just as strings which I tried with x=[str(x) for x in datag.ix[:,0].tolist()[1:6]] . 但是,问题是我想像x=[str(x) for x in datag.ix[:,0].tolist()[1:6]]使用x=[str(x) for x in datag.ix[:,0].tolist()[1:6]]尝试的字符串一样来演示x数据。 Can some one help me to figure out how? 有人可以帮我弄清楚如何吗?

Plotly 'assumes' your data type, even if you provide strings. 即使提供了字符串,也要“假定”您的数据类型。 Setting type to categorical for your xaxis resp. type设置为针对您的xaxis categorical yaxis in layout should do the trick. layout yaxis应该可以解决问题。

import pandas as pd
import plotly.offline as offline
import plotly.plotly as py 
import plotly.graph_objs as go

d = {'x': [None,
           2020.0, 
           5000.0, 
           6010.0, 
           6017.0, 
           6020.0], 
     'y': [None,
           0.005178087393490427,
           0.0014053668695097226,
           0.3174139251746979,
           0.006049724003653125,
           0.24824287385322272]}
datag = pd.DataFrame(data=d)

trace1 = go.Bar(
    x=[str(x) for x in datag.ix[:,0].tolist()[1:6]],
    y=datag.ix[:,1].tolist()[1:6],
    name='travel')

data = [trace1]
layout = go.Layout(
    barmode='stack',
    title='Realization: 0, 0',
    xaxis=dict(title='Model', 
               type='category'),
    yaxis=dict(title='Time (minutes)'))
fig = go.Figure(data=data, layout=layout)
offline.plot(fig, image='png', filename='stacked-bar')

在此处输入图片说明

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

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