python - In plotly, how do I create a linked X axis? -


import matplotlib.pyplot plt import numpy np import plotly.plotly py plotly.graph_objs import *  py.sign_in('uname', 'pass')   trace1 = scatter(     x=[1,2,3,4,5,6,7,8],     y=[24,25,30,21,33,31,30,29],     mode='lines',     xaxis='x1',     )  layout = layout(     title="my first plot",     yaxis=yaxis(         title = "y1"         ),     xaxis=xaxis(         title= 'x1',         anchor = 'x2'         ),     xaxis2=xaxis(         title= 'x2',         side = 'top',         overlaying = 'y'         ),     )  data = [trace1]  fig = figure(data=data, layout=layout)  plot_url = py.plot(fig) 

i trying create second x axis on top of plot (let's call x2). want linked x1 values formula x2=x1*0.3. in matplotlib, define axis , redefine range, , ratio maintained if zoom in/out:

ax2 = ax1.twiny() start, end = ax1.get_xlim() ax2.set_xlim(start*0.3, end*0.3) 

so effect should this: scaled axes in matplotlib

how achieve same effect in plotly?

pretty close! here simple example of multiple x-axes in plotly, adapted this example of multiple y-axes in plotly python

import plotly.plotly py plotly.graph_objs import *  trace1 = scatter(     x=[1,2,3],     y=[24,30,25],     mode='lines',     xaxis='x1', )  trace2 = scatter(     x=[10,20,30],     y=[24,25,30],     mode='lines',     xaxis='x2', )  layout = layout(     title="multiple x-axes",     yaxis=yaxis(         title = "y1"         ),     xaxis=xaxis(         title= 'x-axis 1'     ),     xaxis2=xaxis(         title= 'x-axis 2',         side = 'top',         overlaying='x1'     ) )  data = [trace1, trace2]  fig = figure(data=data, layout=layout)  py.plot(fig, filename='multiple x axes') 

which creates graph: graph multiple x-axes (interactive version: https://plot.ly/~chris/3285)

note can zoom , pan on individual axes: interactivity of plotly graph multiple x-axes

you can specify range of these axes manually, range parameter, maintain ratio zoom in , out scroll. here simple example:

import plotly.plotly py plotly.graph_objs import *  trace1 = scatter(     x=[1,2,3],     y=[24,30,25],     mode='lines',     xaxis='x1', )  trace2 = scatter(     x=[10,20,30],     y=[24,25,30],     mode='lines',     xaxis='x2', )  layout = layout(     title="multiple x-axes",     yaxis=yaxis(         title = "y1"         ),     xaxis=xaxis(         title= 'x-axis 1',         range=[1, 3]     ),     xaxis2=xaxis(         title= 'x-axis 2',         side = 'top',         overlaying='x1',         range=[10, 30]     ) )  data = [trace1, trace2]  fig = figure(data=data, layout=layout)  py.plot(fig, filename='multiple x axes custom range') 

and here graph


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -