3

In Plotly, in order to create scatter plots, I usually do the following:

fig = px.scatter(df, x=x, y=y)
fig.update_xaxes(range=[2, 10])
fig.update_yaxes(range=[2, 10])

I want the yaxis to intersect the xaxis at x=6. So, instead of left yaxis representing negative numbers, I want it to be from [2,6] After the intersection, right side of graph is from [6,10].

Likewise, yaxis from below axis goes from [2,6]. Above the xaxis, it goes from [6,10].

How can I do this in Plotly?

enter image description here

S3DEV
  • 8,768
  • 3
  • 31
  • 42
turtle_in_mind
  • 986
  • 1
  • 18
  • 36
  • 1
    Can you draw an image to illustrate please? Sorry, I don’t follow your logic. – S3DEV Jan 11 '21 at 18:55
  • sure will do it now – turtle_in_mind Jan 11 '21 at 19:09
  • @S3DEV hope that helps – turtle_in_mind Jan 11 '21 at 19:27
  • 1
    @vestland - As far as I’m aware, this isn’t possible as OP describes. Have you run across this before? **rajn** - Personally, I’d set the axes values as required, and leave the axes labels to the left and bottom of the graph; the data will be in the same place regardless of where the axes lines are. You *could* use `shapes` and draw bold horizontal and vertical lines in the middle of the graph, if you like ... – S3DEV Jan 11 '21 at 20:00
  • Sounds good and that’s what I did – turtle_in_mind Jan 11 '21 at 21:21
  • Excellent. I’ll pop an example answer here for you tomorrow, if that’s ok. Just as a reference point, or might spark another idea for you; and perhaps future viewers. Good question. – S3DEV Jan 11 '21 at 22:33
  • @S3DEV Yep, I've seen this before [here](https://stackoverflow.com/questions/55897305/is-there-a-way-to-change-origin-axis-zeroline-on-scatter-graph/55898265#55898265). So this post is strictly speaking a ducplicate. But your answer here (+1) sure is better than mine to the other post, so I'm keeping my close vote for now =) – vestland Jan 12 '21 at 21:37
  • @vestland - Cheers mate, much appreciated. I’ve popped a link to this answer on your previous answer; hope that’s OK. :-) – S3DEV Jan 12 '21 at 22:16
  • @S3DEV Absolutely! And if you're feeling formal wrt forum guidelines I guess we *should* consider marking this post as a duplicate, but I'll at least give OP a chance to mark your suggestion as the accepted answer first. (Actually not sure if thats possible post-dupe-hammering...) – vestland Jan 12 '21 at 22:35

2 Answers2

2

Following on from my comment, as far as I am aware, what you're after is not currently available.

However, here is an example of a work-around which uses a shapes dictionary to add horizontal and vertical lines - acting as intersecting axes - placed at your required x/y intersection of 6.

Sample dataset:

import numpy as np

x = (np.random.randn(100)*2)+6
y1 = (np.random.randn(100)*2)+6
y2 = (np.random.randn(100)*2)+6

Example plotting code:

import plotly.io as pio

layout = {'title': 'Intersection of X/Y Axes Demonstration'}
shapes = []
traces = []

traces.append({'x': x, 'y': y1, 'mode': 'markers'})
traces.append({'x': x, 'y': y2, 'mode': 'markers'})

shapes.append({'type': 'line', 
               'x0': 2, 'x1': 10,
               'y0': 6, 'y1': 6})
shapes.append({'type': 'line', 
               'x0': 6, 'x1': 6,
               'y0': 2, 'y1': 10})

layout['shapes'] = shapes
layout['xaxis'] = {'range': [2, 10]}
layout['yaxis'] = {'range': [2, 10]}

pio.show({'data': data, 'layout': layout})

Output:

enter image description here

Comments (TL;DR):

The example code shown here uses the low-level Plotly API (plotly.io), rather than a convenience wrapper such as graph_objects or express. The reason is that I (personally) feel it's helpful to users to show what is occurring 'under the hood', rather than masking the underlying code logic with a convenience wrapper.

This way, when the user needs to modify a finer detail of the graph, they will have a better understanding of the lists and dicts which Plotly is constructing for the underlying graphing engine (orca).

S3DEV
  • 8,768
  • 3
  • 31
  • 42
0

I think fig.add_hline() and fig.add_vline() is the function your need.

Example code

import plotly.express as px
import pandas as pd
df = pd.DataFrame({'x':[6,7,3], 'y':[4,5,6]})
fig = px.scatter(df, x='x', y='y')
fig.update_xaxes(range=[2, 10])
fig.update_yaxes(range=[2, 10])
fig.add_hline(y=4)
fig.add_vline(x=6)
fig.show()

Output plotly with shifted axes

mosc9575
  • 5,618
  • 2
  • 9
  • 32