简体   繁体   English

绘图布局-Y轴范围不起作用

[英]plotly layout - Y Axis range not working

i am trying to create a graph that will show a Scatter of the times i opened my computer. 我正在尝试创建一个图表,以显示我打开计算机时的时间分散情况。 - i am using plotly offline. -我正在离线使用。 Y = time, X = date. Y =时间,X =日期。

this works fine, but i can't make the Y axis to show all the hours of the day, but only the range of times that was in the data. 这工作正常,但我无法使Y轴显示一天中的所有时间,而仅显示数据中的时间范围。

(for example, if i opened my computer only between 13:00-15:00 every day, the range of the Y axis will be from 13:00 to 15:00 only) (例如,如果我每天仅在13:00-15:00之间打开计算机,则Y轴的范围将仅从13:00到15:00)

i am trying to show all the hours of the day on the Y axis by using the 'range' attribute in the layout, but it still doesn't work for me. 我试图通过使用布局中的'range'属性在Y轴上显示一天中的所有时间,但是它仍然对我不起作用。

an example of my code: 我的代码示例:

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *
from datetime import datetime, time

init_notebook_mode(connected=True)

# in the example i am using fabricated data. declaring the data:
time_data = [datetime(2017, 07, 28, 21, 37, 19), datetime(2017, 07, 29, 17, 11, 56), datetime(2017, 08, 01, 11, 15, 45), datetime(2017, 08, 02, 13, 54, 03)]
x_data = []
y_data = []

# creating the x-row data with dates only, and the y-row data with the time only
for row in time_data:
    x_data.append(row.date())
    y_data.append(datetime.combine(datetime(2017, 1, 1).date(), row.time))

#declaring the data for the graph
data = [Scatter(x=x_data, y=y_data, name='Times On', mode='markers')]

# creating the hour range 
hours = []
for i in range (0, 24):
    hours.append(datetime(2017, 1, 1, i, 0, 0))

# declaring the Layout with the 'range' attribute, and Figure
layout = dict(title='Times On', xaxis=dict(type='date'), yaxis={'type': 'date', 'tickformat': '%H:%M', 'range': hours})
fig = Figure(data=data, layout=layout)

# plotting
plot(figure_or_data=fig, filename='C:\Users\tototo\Desktop\Time-On')

does someone know what's the problem? 有人知道出什么问题吗? any help would be blessed! 任何帮助都会很幸运! Thanks! 谢谢!

plotly seems to limit the axis based on the max and min values present in the corresponding axis. 似乎根据相应轴中存在的最大值和最小值来限制轴。 I tried each of the properties and came up with a solution. 我尝试了每个属性,并提出了解决方案。

Approach: The first one is generating what you need, but cant seem to get it to start at 12 in the midnight and end at 12 the next day. 方法:第一个是生成所需的内容,但似乎无法使它从午夜的12点开始到第二天的12点结束。

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *
from datetime import datetime, time

init_notebook_mode(connected=True)

# in the example i am using fabricated data. declaring the data:
time_data = [datetime(2017, 7, 28, 21, 37, 19), datetime(2017, 7, 29, 17, 11, 56), datetime(2017,8, 1, 11, 15, 45), datetime(2017, 8, 2, 13, 54, 3)]
x_data = []
y_data = []

# creating the x-row data with dates only, and the y-row data with the time only
for row in time_data:
    x_data.append(row.date())
    y_data.append(str(datetime.combine(datetime(2017, 1, 1).date(), row.time())))

#declaring the data for the graph
data = [Scatter(x=x_data, y=y_data, name='Times On', mode='markers')]

# creating the hour range 
hours = []
for i in range (0, 24):
    hours.append(datetime(2017, 1, 1, i, 0, 0))

# declaring the Layout with the 'range' attribute, and Figure
layout = dict(title='Times On', xaxis=dict(type='date'), yaxis={'type': 'date', 'tickformat': '%H:%M', 
                                                                'nticks': 30, 'tick0': hours[0],
                                                                'range': [hours[0], hours[len(hours)-1]],
                                                                'autorange': False})
fig = Figure(data=data, layout=layout)

# plotting
iplot(figure_or_data=fig)

The above code is giving me the output: 上面的代码给了我输出:

在此处输入图片说明

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

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