情节:如何更改x轴的值格式? [英] Plotly: How to change the format of the values for the x axis?

查看:76
本文介绍了情节:如何更改x轴的值格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用python从数据创建图形.

我从各种网站中汲取了灵感,并编写了此脚本:

  import plotly.express as px导入plotly.graph_objs导入statsmodels.api作为sm值= [1、2、3、4、5、5、5、6、6、7、8]日期= [2010、2011、2012、2013、2014、2015、2016、2017、2018、2019、2020]无花果= px.scatter(x = date,y = value)fig.add_trace(go.Scatter(x = date,y = value,mode ='lines',name ='MB Used'))趋势= sm.OLS(值,sm.add_constant(日期)).fit().拟合值fig.add_traces(go.Scatter(x = date,y = trend,mode ='lines',name ='trendline'))图 

此脚本允许生成此图:

对于x轴,我想显示类似 2020-01-01-06:00 的值,但是当我像这样更改列表时:

  date = [2020-01-01-06:00,2020-01-01-12:00,2020-01-01-18:00,2020-01-02-06:00,2020-01-02-12:00、2020-01-02-18:00、2020-01-03-06:00、2020-01-03-12:00、2020-01-03-18:00、2020-01-04-06:00,2020-01-04-12:00] 

错误是:

 文件< ipython-input-13-4958920545c3>",第6行日期= [2020-01-01-06:00、2020-01-01-12:00、2020-01-01-18:00、2020-01-02-06:00、2020-01-02-12:00,2020-01-02-18:00,2020-01-03-06:00,2020-01-03-12:00,2020-01-03-18:00,2020-01-04-06:00,2020-01-04-12:00]^语法错误:无效的令牌 

如果我尝试这样做:

  date = ['2020-01-01-06:00','2020-01-01-12:00','2020-01-01-18:00','2020-01-02-06:00','2020-01-02-12:00','2020-01-02-18:00','2020-01-03-06:00','2020-01-03-12:00','2020-01-03-18:00','2020-01-04-06:00','2020-01-04-12:00'] 

错误是:

 ---------------------------------------------------------------------------TypeError Traceback(最近一次通话)< ipython-input-15-e06e438ca2eb>在< module>中10 fig.add_trace(go.Scatter(x = date,y = value,mode ='lines',name ='MB Used'))11--->12个趋势= sm.OLS(value,sm.add_constant(date)).fit().fittedvalues1314图.add_traces(go.Scatter(x = date,y = trend,mode ='lines',name ='trendline'))〜/.local/lib/python3.6/site-packages/statsmodels/tools/tools.py在add_constant(data,prepend,has_constant)中303提高ValueError('仅实现的二维数组')304->305 is_nonzero_const = np.ptp(x,axis = 0)== 0306 is_nonzero_const& = np.all(x!= 0.0,轴= 0)307 if is_nonzero_const.any():< __ array_function__内部构件>在ptp(* args,** kwargs)中〜/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py in ptp(a,axis,out,keepdims)2541其他:2542返回ptp(axis = axis,out = out,** kwargs)->2543 return _methods._ptp(a,axis = axis,out = out,** kwargs)25442545〜/.local/lib/python3.6/site-packages/numpy/core/_methods.py in _ptp(a,axis,out,keepdims)228 def _ptp(a,轴=无,out =无,keepdims = False):第229章->230 umr_maximum(a,axis,None,out,keepdims),231 umr_minimum(a,axis,None,None,keepdims),232出TypeError:无法使用灵活类型执行归约 

请,你能告诉我如何更改吗?

解决方案

答案:

在以下代码段中,我已经在

详细信息:

有几个原因导致您无法在提供的代码段中获得理想的结果.首先,构造日期和时间值列表的任何尝试都无法通过此处应用的功能轻松识别.在 date = ['2020-01-01-06:00','2020-01-01-12:00',...] 中,您应删除一个连字符以获取 ['2020-01-01 06:00','2020-01-01 12:00'...] 代替.但是,即使有更广泛认可的时间戳列表,据我所知statsmodels也不会接受 sm.OLS()中的时间戳.最后,在非标准的x轴刻度上应用明智的标签可能是密谋的真正挑战(非常之一).

请不要因为网格线的不规则外观反映了数据的结构.您缺少对以 00-00-00 结尾的时间戳表示24小时周期的观察结果.

代码:

 #个导入导入plotly.express为px导入plotly.graph_objs导入statsmodels.api作为sm导入日期时间为dt# 数据值= [1、2、3、4、5、5、5、6、6、7、8]日期= [2010、2011、2012、2013、2014、2015、2016、2017、2018、2019、2020]date_h = ['2020-01-01 06:00','2020-01-01 12:00','2020-01-01 18:00','2020-01-02 06:00','2020-01-02 12:00','2020-01-02 18:00','2020-01-03 06:00','2020-01-03 12:00','2020-01-03 18:00','2020-01-04 06:00','2020-01-04 12:00']#在pandas数据框中组织数据df = pd.DataFrame({'value':value,'date':日期,'date_h':pd.to_datetime(date_h)})#函数对不规则的时间戳进行序列化def serial_date(date1):temp = dt.datetime(1899,12,30)#注意,不是12月31日,而是30日!增量= date1-临时返回float(delta.days)+(float(delta.seconds)/86400)df ['date_s'] = [df中的d的serial_date(d)['date_h']]#设置基准图无花果= px.scatter(x = df ['date_s'],y = df ['value'])fig.add_trace(go.Scatter(x = df ['date_s'],y = df ['value'],mode ='lines',name ='MB Used'))#使用sm.OLS进行线性回归的设置Y = df ['value']独立= ['date_s']X = df [独立]X = sm.add_constant(X)#估计趋势趋势= sm.OLS(Y,X).fit().fittedvalues#在图上添加趋势线fig.add_traces(go.Scatter(x = df ['date_s'],y = trend,mode ='lines',name ='trendline'))#指定tick0,tickvals和ticktext以实现所需的x轴格式fig.update_layout(yaxis = dict(title =''),xaxis = dict(title ='',tick0 = df ['date_s'].iloc [0],tickvals = df ['date_s'],ticktext = df ['date_h']))图show() 

I need to create a graph from data with python.

I took my inspiration from various website and I've made this script :

import plotly.express as px
import plotly.graph_objs as go
import statsmodels.api as sm

value = [1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 8]
date = [ 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]

fig = px.scatter(x=date, y=value )
fig.add_trace(go.Scatter(x=date, y=value, mode='lines',name='MB Used' ))

trend = sm.OLS(value,sm.add_constant(date)).fit().fittedvalues

fig.add_traces(go.Scatter(x=date, y=trend,mode = 'lines', name='trendline'))
fig

This script allow to generate this graph :

For the x axe, I would like to display the value like that 2020-01-01-06:00 but when I change my list like that :

date = [ 2020-01-01-06:00, 2020-01-01-12:00, 2020-01-01-18:00, 2020-01-02-06:00, 2020-01-02-12:00, 2020-01-02-18:00, 2020-01-03-06:00, 2020-01-03-12:00, 2020-01-03-18:00, 2020-01-04-06:00, 2020-01-04-12:00 ]

The error is :

File "<ipython-input-13-4958920545c3>", line 6
    date = [ 2020-01-01-06:00, 2020-01-01-12:00, 2020-01-01-18:00, 2020-01-02-06:00, 2020-01-02-12:00, 2020-01-02-18:00, 2020-01-03-06:00, 2020-01-03-12:00, 2020-01-03-18:00, 2020-01-04-06:00, 2020-01-04-12:00 ]
                   ^
SyntaxError: invalid token

If I try that :

date = [ '2020-01-01-06:00', '2020-01-01-12:00', '2020-01-01-18:00', '2020-01-02-06:00', '2020-01-02-12:00', '2020-01-02-18:00', '2020-01-03-06:00', '2020-01-03-12:00', '2020-01-03-18:00', '2020-01-04-06:00', '2020-01-04-12:00' ]

The error is :


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-e06e438ca2eb> in <module>
     10 fig.add_trace(go.Scatter(x=date, y=value, mode='lines',name='MB Used' ))
     11 
---> 12 trend = sm.OLS(value,sm.add_constant(date)).fit().fittedvalues
     13 
     14 fig.add_traces(go.Scatter(x=date, y=trend,mode = 'lines', name='trendline'))

~/.local/lib/python3.6/site-packages/statsmodels/tools/tools.py in add_constant(data, prepend, has_constant)
    303         raise ValueError('Only implementd 2-dimensional arrays')
    304 
--> 305     is_nonzero_const = np.ptp(x, axis=0) == 0
    306     is_nonzero_const &= np.all(x != 0.0, axis=0)
    307     if is_nonzero_const.any():

<__array_function__ internals> in ptp(*args, **kwargs)

~/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py in ptp(a, axis, out, keepdims)
   2541         else:
   2542             return ptp(axis=axis, out=out, **kwargs)
-> 2543     return _methods._ptp(a, axis=axis, out=out, **kwargs)
   2544 
   2545 

~/.local/lib/python3.6/site-packages/numpy/core/_methods.py in _ptp(a, axis, out, keepdims)
    228 def _ptp(a, axis=None, out=None, keepdims=False):
    229     return um.subtract(
--> 230         umr_maximum(a, axis, None, out, keepdims),
    231         umr_minimum(a, axis, None, None, keepdims),
    232         out

TypeError: cannot perform reduce with flexible type

Please, could you show me how to change that ?

解决方案

The answer:

In the following code snippet I've replaced your dates with floats following this approach to serialize timestamps. This way you can use your dates both as input to sm.OLS and as one of a few more steps to get your dates displayed in the figure with your desired format.

The plot:

The details:

There are several reasons why you are not getting your desired result in your provided code snippet. First of all, none of the attempts of constuctring lists of date and time values are easily recognizable by the functions you are applying here. In date = [ '2020-01-01-06:00', '2020-01-01-12:00',...] you should remove one of the hyphens to get ['2020-01-01 06:00', '2020-01-01 12:00'...] instead. But even with a more widely recognizable list of timestamps, statsmodels will to my knowledge not accept those in sm.OLS(). And in the end, applying sensible labels to non-standard x-axis tickmarks can be (one of very few) real challenges in plotly.

Please not that the irregegular appearances of gridlines reflect the structure of your data. You're missing observations for timestamps that end with 00-00-00 to represent a 24 hour cycle.

The code:

# imports
import plotly.express as px
import plotly.graph_objs as go
import statsmodels.api as sm
import datetime as dt

# data
value = [1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 8]
date = [ 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
date_h = ['2020-01-01 06:00', '2020-01-01 12:00', '2020-01-01 18:00', '2020-01-02 06:00', '2020-01-02 12:00', '2020-01-02 18:00', '2020-01-03 06:00', '2020-01-03 12:00', '2020-01-03 18:00', '2020-01-04 06:00', '2020-01-04 12:00' ]

# organize data in a pandas dataframe
df = pd.DataFrame({'value':value,
                   'date':date,
                    'date_h':pd.to_datetime(date_h)})

# function to serilaize irregular timestmps
def serial_date(date1):
    temp = dt.datetime(1899, 12, 30)    # Note, not 31st Dec but 30th!
    delta = date1 - temp
    return float(delta.days) + (float(delta.seconds) / 86400)

df['date_s'] = [serial_date(d) for d in df['date_h']]

# set up base figure
fig = px.scatter(x=df['date_s'], y=df['value'] )
fig.add_trace(go.Scatter(x=df['date_s'], y=df['value'], mode='lines',name='MB Used' ))

# setup for linear regression using sm.OLS
Y=df['value']
independent=['date_s']
X=df[independent]
X=sm.add_constant(X)

# estimate trend
trend = sm.OLS(Y,X).fit().fittedvalues

# add trendline to figure
fig.add_traces(go.Scatter(x=df['date_s'], y=trend,mode = 'lines', name='trendline'))

# specify tick0, tickvals and ticktext to achiece desired x-axis format
fig.update_layout(yaxis=dict(title=''),
                  xaxis=dict(title='',
                  tick0= df['date_s'].iloc[0],
                  tickvals= df['date_s'],
                  ticktext = df['date_h'])
                 )

fig.show()

这篇关于情节:如何更改x轴的值格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆