我正在尝试创建一个图表,显示我打开计算机的时间分散。 - 我正在使用情节离线。 Y =时间, X =日期。
这很好用,但我不能让Y轴显示一天中的所有小时数,而只能显示数据中的时间范围。
(例如,如果我每天只在13:00-15:00之间打开电脑,则Y轴的范围仅为13:00至15:00)
我试图通过在布局中使用'range'属性来显示Y轴上的所有时间,但它仍然不适合我。
我的代码示例:
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')
有人知道这是什么问题吗?任何帮助都会很幸运! 谢谢!
答案 0 :(得分:0)
图示似乎基于相应轴中存在的最大值和最小值来限制轴。我尝试了每个属性并提出了解决方案。
方法:第一个产生你需要的东西,但似乎不能让它从午夜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)
上面的代码给出了输出: