在工作日名称中标出xaxis [英] Plotly xaxis in weekday name
问题描述
我查看了文档,但他们似乎没有提及它.
I’ve looked into the documentation but they didn’t seem to mention it.
如何更改x轴上的标签以显示周一20-7",周二21-7"等. 用于xaxis的日期"格式为"20-7-2018 11:00:00 am",等等.
How can I change the label on x axis to show "Mon 20-7", "Tue 21-7", etc. The 'date' used for xaxis is in the format "20-7-2018 11:00:00am", etc.
我使用以下Python Plotly脚本:
I use the following Python Plotly script:
trace0=go.Scatter(x=df_pre.index,y=df_pre['Total'],line=dict(color=('rgb(16,25,109)'),width=1),name='Period_1')
trace1=go.Scatter(x=df_post.index,y=df_post['Total'],line=dict(color=('rgb(77,221,26)'),width=2),name='Period_2')
data=[trace0,trace1]
layout=dict(title='Total',width=960,height=768,
yaxis=dict(title='Avg',ticklen=5,zeroline=False,gridwidth=2,),
xaxis=dict(title='Date',ticklen=5,zeroline=False,gridwidth= 2,))
fig=dict(data=data,layout=layout)
iplot(fig,filename='Total')
任何帮助将不胜感激
推荐答案
如果您想在xaxis上看到"Tue 14-08",请按照以下步骤操作(在下面的代码中添加):
If you want to see "Tue 14-08" on xaxis, follow those steps(added in code below):
1.创建一个与您的需求相对应的列
1.Create a column which corresponds to your requirements
df_pre["date2"] = df_pre["date"].apply(lambda x: datetime.datetime.\
strptime(x,"%d-%m-%Y %I:%M:%S%p").strftime("%a %d-%m"))
print(df_pre["date2"])
0 Tue 14-08
1 Wed 15-08
2 Thu 16-08
3 Fri 17-08
4 Sat 18-08
Name: date2, dtype: object
2.从看起来像要在xaxis(df_pre["date2"]
)上看到的列中创建一个list
2.Create a list
from column that looks like you want to see it on xaxis (df_pre["date2"]
)
list = df_pre["date2"].tolist()
print(list)
['Tue 14-08', 'Wed 15-08', 'Thu 16-08', 'Fri 17-08', 'Sat 18-08']
3.在xaxis Layout
中放入两个参数tickvals
和ticktext
:在第一个参数中,要迭代多少个值.然后在第二个参数中选择文本(例如,我们从列df["date2"]
中获得的list
)
3.Put in xaxis Layout
two parameters tickvals
and ticktext
: in first parameter over how many values you want to iterate. And in second parameter choose you text (such as this list
that we get from column df["date2"]
)
layout=dict(title="Total",width=960,height=768,
yaxis=dict(title="Avg",ticklen=5,zeroline=False,gridwidth=2),
xaxis=dict(title="Date",ticklen=5,zeroline=False,gridwidth=2,
#Choose what you want to see on xaxis! In this case list
tickvals=[i for i in range(len(list))],
ticktext=list
))
输出应该是这样的:
And output should be something like that:
我在文档中找不到您需要的选项.但是请不要忘记,您可以使用Python
和pandas
准备要放入x
中的数据:
I am can not find the option in documentation, that your need. But do not forgot, you can prepare data that you after can put in x
, using Python
and pandas
:
#Import all what we need
import pandas as pd
import plotly
import plotly.graph_objs as go
#Create first DataFrame
df_pre = pd.DataFrame({"date":["14-08-2018 11:00:00am",
"15-08-2018 12:00:00am",
"16-08-2018 01:00:00pm",
"17-08-2018 02:00:00pm",
"18-08-2018 03:00:00pm"],
"number":["3","5","10","18","22"]})
#Create a column which corresponds to your requirements
df_pre["dow"] = pd.to_datetime(df_pre["date"], \
format="%d-%m-%Y %I:%M:%S%p").dt.weekday_name
df_pre["firstchunk"] = df_pre["dow"].astype(str).str[0:3]
df_pre["lastchunk"] = df_pre["date"].astype(str).str[0:5]
df_pre["final"] = df_pre["firstchunk"] + " " + df_pre["lastchunk"]
#Check DataFrame
print(df_pre)
#Repeat all the actions above to the second DataFrame
df_post = pd.DataFrame({"date":["14-08-2018 11:00:00am",
"15-08-2018 12:00:00am",
"16-08-2018 01:00:00pm",
"17-08-2018 02:00:00pm",
"18-08-2018 03:00:00pm"],
"number":["6","8","12","19","23"]})
df_post["dow"] = pd.to_datetime(df_post["date"], \
format="%d-%m-%Y %I:%M:%S%p").dt.weekday_name
df_post["firstchunk"] = df_post["dow"].astype(str).str[0:3]
df_post["lastchunk"] = df_post["date"].astype(str).str[0:5]
df_post["final"] = df_post["firstchunk"] + " " + df_post["lastchunk"]
print(df_post)
#Create list that needed to xaxis
list = df_pre["final"].tolist()
print(list)
#Prepare data
trace0=go.Scatter(x=df_pre["date"],y=df_pre["number"],
line=dict(color=("rgb(16,25,109)"),width=1),name="Period_1")
trace1=go.Scatter(x=df_post["date"],y=df_post["number"],
line=dict(color=("rgb(77,221,26)"),width=2),name="Period_2")
data = [trace0,trace1]
#Prepare layout
layout=dict(title="Total",width=960,height=768,
yaxis=dict(title="Avg",ticklen=5,zeroline=False,gridwidth=2),
xaxis=dict(title="Date",ticklen=5,zeroline=False,gridwidth=2,
#Choose what you want to see on xaxis! In this case list
tickvals=[i for i in range(len(list))],
ticktext=list
))
fig = go.Figure(data=data, layout=layout)
#Save plot as "Total.html" in directory where your script is
plotly.offline.plot(fig, filename="Total.html", auto_open = False)
更新:您也可以尝试使用datetime
实现所需的功能(更简单):
Update: Also you can try using datetime
to achieve what you want (that`s more simple):
#Import all what we need
import pandas as pd
import plotly
import plotly.graph_objs as go
import datetime
#Create first DataFrame
df_pre = pd.DataFrame({"date":["14-08-2018 11:00:00am",
"15-08-2018 12:00:00am",
"16-08-2018 01:00:00pm",
"17-08-2018 02:00:00pm",
"18-08-2018 03:00:00pm"],
"number":["3","5","10","18","22"]})
#Create a column which corresponds to your requirements
df_pre["date2"] = df_pre["date"].apply(lambda x: datetime.datetime.\
strptime(x,"%d-%m-%Y %I:%M:%S%p").strftime("%a %d-%m"))
#Check DataFrame
print(df_pre)
#Repeat all the actions above to the second DataFrame
df_post = pd.DataFrame({"date":["14-08-2018 11:00:00am",
"15-08-2018 12:00:00am",
"16-08-2018 01:00:00pm",
"17-08-2018 02:00:00pm",
"18-08-2018 03:00:00pm"],
"number":["6","8","12","19","23"]})
df_post["date2"] = df_post["date"].apply(lambda x: datetime.datetime.\
strptime(x,'%d-%m-%Y %I:%M:%S%p').strftime("%a %d-%m"))
print(df_post)
#Create list that needed to xaxis
list = df_pre["date2"].tolist()
print(list)
#Prepare data
trace0=go.Scatter(x=df_pre["date"],y=df_pre["number"],
line=dict(color=("rgb(16,25,109)"),width=1),name="Period_1")
trace1=go.Scatter(x=df_post["date"],y=df_post["number"],
line=dict(color=("rgb(77,221,26)"),width=2),name="Period_2")
data = [trace0,trace1]
#Prepare layout
layout=dict(title="Total",width=960,height=768,
yaxis=dict(title="Avg",ticklen=5,zeroline=False,gridwidth=2),
xaxis=dict(title="Date",ticklen=5,zeroline=False,gridwidth=2,
#Choose what you want to see on xaxis! In this case list
tickvals=[i for i in range(len(list))],
ticktext=list
))
fig = go.Figure(data=data, layout=layout)
#Save plot as "Total.html" in directory where your script is
plotly.offline.plot(fig, filename="Total.html")
这篇关于在工作日名称中标出xaxis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!