背景:
此问题与Plotly: How to retrieve values for major ticks and gridlines?相关,但不相同。也有人在此处matplotlib
提出了类似的问题,但没有回答。Plotly非常棒,也许唯一困扰我的是刻度线/网格线的自动选择以及为x轴选择的标签,如下图所示:
情节1:
How do I show major ticks as the first day of each months and minor ticks as each day?
我认为此处自然显示的是 每月的第一天 (取决于课程的期限)。或者,甚至在每个刻度上都仅使用一个缩写的月份名称,例如'Jan'
。由于所有月份的长度都不相同,因此我意识到了技术上甚至是视觉上的挑战。但是有人知道怎么做吗?
可复制的代码段:
import plotly
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np
from IPython.display import HTML
from IPython.core.display import display, HTML
import copy
# setup
init_notebook_mode(connected=True)
np.random.seed(123)
cf.set_config_file(theme='pearl')
# Random data using cufflinks
df = cf.datagen.lines()
#df = df['UUN.XY']
fig = df.iplot(asFigure=True, kind='scatter',
xTitle='Dates',yTitle='Returns',title='Returns')
iplot(fig)
答案 0 :(得分:0)
解决方案:
如何设置网格线完全取决于您要显示的内容,以及在之前如何构建图形,然后尝试编辑设置。但是要获得问题中指定的结果,您可以这样做。
第1步:
为fig['data'][series]['x']
中的每个系列编辑fig['data']
。
第二步:
在以下位置设置tickmode和ticktext:
go.Layout(xaxis = go.layout.XAxis(tickvals = [some_values]
ticktext = [other_values])
)
结果:
Jupyter Notebook的完整代码:
# imports
import plotly
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np
from IPython.display import HTML
from IPython.core.display import display, HTML
import copy
import plotly.graph_objs as go
# setup
init_notebook_mode(connected=True)
np.random.seed(123)
cf.set_config_file(theme='pearl')
#%qtconsole --style vim
# Random data using cufflinks
df = cf.datagen.lines()
# create figure setup
fig = df.iplot(asFigure=True, kind='scatter',
xTitle='Dates',yTitle='Returns',title='Returns')
# create df1 to mess around with while
# keeping the source intact in df
df1 = df.copy(deep = True)
df1['idx'] = range(0, len(df))
# time variable operations and formatting
df1['yr'] = df1.index.year
df1['mth'] = df1.index.month_name()
# function to replace month name with
# abbreviated month name AND year
# if the month is january
def mthFormat(month):
dDict = {'January':'jan','February':'feb', 'March':'mar',
'April':'apr', 'May':'may','June':'jun', 'July':'jul',
'August':'aug','September':'sep', 'October':'oct',
'November':'nov', 'December':'dec'}
mth = dDict[month]
return(mth)
# replace month name with abbreviated month name
df1['mth'] = [mthFormat(m) for m in df1['mth']]
# remove adjacent duplicates for year and month
df1['yr'][df1['yr'].shift() == df1['yr']] = ''
df1['mth'][df1['mth'].shift() == df1['mth']] = ''
# select and format values to be displayed
df1['idx'][df1['mth']!='']
df1['display'] = df1['idx'][df1['mth']!='']
display = df1['display'].dropna()
displayVal = display.values.astype('int')
df_display = df1.iloc[displayVal]
df_display['display'] = df_display['display'].astype('int')
df_display['yrmth'] = df_display['mth'] + '<br>' + df_display['yr'].astype(str)
# set properties for each trace
for ser in range(0,len(fig['data'])):
fig['data'][ser]['x'] = df1['idx'].values.tolist()
fig['data'][ser]['text'] = df1['mth'].values.tolist()
fig['data'][ser]['hoverinfo']='all'
# layout for entire figure
f2Data = fig['data']
f2Layout = go.Layout(
xaxis = go.layout.XAxis(
tickmode = 'array',
tickvals = df_display['display'].values.tolist(),
ticktext = df_display['yrmth'].values.tolist(),
zeroline = False)#,
)
# plot figure with specified major ticks and gridlines
fig2 = go.Figure(data=f2Data, layout=f2Layout)
iplot(fig2)
一些重要的详细信息:
1。 iplot()
的灵活性和局限性:
这种使用iplot()
并编辑所有设置的方法有点笨拙,但是在数据集中列/变量的数量方面非常灵活,可以说比像{{1 }},用于df中的每一列。
2。为什么必须编辑每个系列/轨迹?
如果您尝试跳过中间部分
trace1 = go.Scatter()
并尝试在整个图上直接设置for ser in range(0,len(fig['data'])):
fig['data'][ser]['x'] = df1['idx'].values.tolist()
fig['data'][ser]['text'] = df1['mth'].values.tolist()
fig['data'][ser]['hoverinfo']='all'
和tickvals
,它将无效:
我认为这有点奇怪,但是我认为这是由ticktext
启动的一些基础设置引起的。
3。仍然缺少一件事:
为了使设置正常工作,iplot()
和ticvals
的结构分别为ticktext
和[0, 31, 59, 90]
。这会导致x轴行悬停文本显示['jan<br>2015', 'feb<br>', 'mar<br>', 'apr<br>']
和ticvals
为空的数据位置:
任何有关如何改善整体效果的建议均受到高度赞赏。比我自己的解决方案更好的解决方案将立即获得 已接受答案 状态!