gpt4 book ai didi

python - 使用plotly创建条形图

转载 作者:行者123 更新时间:2023-12-01 02:00:59 25 4
gpt4 key购买 nike

我正在尝试使用plotly创建条形图。

输入数据如下所示:

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

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

代码是

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')

我得到以下输出: enter image description here但是,问题是我想将 x 数据演示为字符串我尝试使用 x=[str(x) for x in datag.ix[:,0].tolist()[1:6]] 。有人可以帮我弄清楚怎么做吗?

最佳答案

即使您提供了字符串,Plotly 也会“假定”您的数据类型。设置type为您的 xaxis 分别设置为categoricallayout 中的 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')

enter image description here

关于python - 使用plotly创建条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49628849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com