Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subplot_titles posisiong value goes negative when shared axes is True and start_cell is bottom-left #1002

Closed
rysktky opened this issue May 6, 2018 · 4 comments
Labels
bug something broken

Comments

@rysktky
Copy link

rysktky commented May 6, 2018

Hi I just fund code like this does not work as described on the title.
I could not find anything relevant about this.
I am trying to find a workaround, but could there be simple solution?

using python3.6 with fedora 28.
installed plotly with pip.

Regards

sample code
#Added "vertical_spacing=0.00" to simplified the calculation as per plotly.py/plotly/tootls.py

from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go

trace0 = go.Scatter( x=[1, 2], y=[1, 2])
trace1 = go.Scatter( x=[1, 2], y=[5, 3])
trace2 = go.Scatter( x=[1, 2, 3], y=[2, 1, 2])

fig = tools.make_subplots(rows=3, cols=1, 
                          shared_xaxes=True,
                          start_cell='bottom-left',
                          vertical_spacing=0.00,
                          subplot_titles=('First Subplot','Second Subplot', 'Third Subplot'))

fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 3, 1)

fig['layout'].update(showlegend=False, title='Specs with Subplot Title')
py.plot(fig, filename='custom-sized-subplot-with-subplot-titles')

pprint'ed the layout

>>> pprint.pprint(fig['layout'])
{'annotations': [{'font': {'size': 16},
                  'showarrow': False,
                  'text': 'First Subplot',
                  'x': 0.5,
                  'xanchor': 'center',
                  'xref': 'paper',
                  'y': 1.0,     <<<< This is GOOD.
                  'yanchor': 'bottom',
                  'yref': 'paper'},
                 {'font': {'size': 16},
                  'showarrow': False,
                  'text': 'Second Subplot',
                  'x': 0.5,
                  'xanchor': 'center',
                  'xref': 'paper',
                  y': 0.0,     <<< 2nd subplot title is already at ZERO and it is shown on the bottom.
                  'yanchor': 'bottom',
                  'yref': 'paper'},
                 {'font': {'size': 16},
                  'showarrow': False,
                  'text': 'Third Subplot',
                  'x': 0.5,
                  'xanchor': 'center',
                  'xref': 'paper',
                  'y': -1.0,     <<< It has negative value and does not get actually shown.
                  'yanchor': 'bottom',
                  'yref': 'paper'}],
 'xaxis1': {'anchor': 'y1', 'domain': [0.0, 1.0]},
 'yaxis1': {'anchor': 'x1', 'domain': [0.0, 0.3333333333333333]},
 'yaxis2': {'anchor': 'free',
            'domain': [0.3333333333333333, 0.6666666666666666],
            'position': 0.0},
 'yaxis3': {'anchor': 'free',
            'domain': [0.6666666666666666, 1.0],
            'position': 0.0}}

It seems it is coming from this part of plotly.py/plotly/tootls.py

1303     # Add subplot titles
1304 
1305     # If shared_axes is False (default) use list_of_domains
1306     # This is used for insets and irregular layouts
1307     if not shared_xaxes and not shared_yaxes:
1308         x_dom = list_of_domains[::2]
1309         y_dom = list_of_domains[1::2]
1310         subtitle_pos_x = []
1311         subtitle_pos_y = []
1312         for x_domains in x_dom:
1313             subtitle_pos_x.append(sum(x_domains) / 2)
1314         for y_domains in y_dom:
1315             subtitle_pos_y.append(y_domains[1])
1316     # If shared_axes is True the domin of each subplot is not returned so the
1317     # title position must be calculated for each subplot
1318     else:
1319         subtitle_pos_x = [None] * cols
1320         subtitle_pos_y = [None] * rows
1321         delt_x = (x_e - x_s)
1322         for index in range(cols):
1323             subtitle_pos_x[index] = ((delt_x / 2) +
1324                                      ((delt_x + horizontal_spacing) * index))
1325         subtitle_pos_x *= rows
1326         for index in range(rows):
1327             subtitle_pos_y[index] = (1 - ((y_e + vertical_spacing) * index))
1328         subtitle_pos_y *= cols
1329         subtitle_pos_y = sorted(subtitle_pos_y, reverse=True) 

How the result look like
Please remember it has vertical_spacing=0.00. There are 3 subplots stacked vertically here...
newplot

@rysktky
Copy link
Author

rysktky commented May 7, 2018

I guess this works

Could some one verify too?

--- a/plotly/tools.py
+++ b/plotly/tools.py
@@ -1324,9 +1324,15 @@ def make_subplots(rows=1, cols=1,
                                      ((delt_x + horizontal_spacing) * index))
         subtitle_pos_x *= rows
         for index in range(rows):
-            subtitle_pos_y[index] = (1 - ((y_e + vertical_spacing) * index))
+            if ROW_DIR > 0:
+                subtitle_pos_y[index] = (1 - ((y_e/rows + vertical_spacing/rows) * index))
+            else:
+                subtitle_pos_y[index] = (1 - ((y_e + vertical_spacing) * index))
         subtitle_pos_y *= cols
-        subtitle_pos_y = sorted(subtitle_pos_y, reverse=True)
+        if ROW_DIR > 0:
+            subtitle_pos_y = sorted(subtitle_pos_y, reverse=False)
+        else:
+            subtitle_pos_y = sorted(subtitle_pos_y, reverse=True)

     plot_titles = []
     for index in range(len(subplot_titles)):

@rysktky
Copy link
Author

rysktky commented May 9, 2018

Just opened pullrequest #1005 for this.

@jonmmease jonmmease added the bug something broken label Sep 22, 2018
@jonmmease
Copy link
Contributor

This will be fixed in version 4 by #1528.

You can try out the new behavior using the v4_subplot future flag.

from _plotly_future_ import v4_subplots
from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go

trace0 = go.Scatter( x=[1, 2], y=[1, 2])
trace1 = go.Scatter( x=[1, 2], y=[5, 3])
trace2 = go.Scatter( x=[1, 2, 3], y=[2, 1, 2])

fig = tools.make_subplots(rows=3, cols=1, 
                          shared_xaxes=True,
                          start_cell='bottom-left',
                          vertical_spacing=0.00,
                          subplot_titles=('First Subplot','Second Subplot', 'Third Subplot'))

fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 3, 1)

fig['layout'].update(showlegend=False, title='Specs with Subplot Title')
py.plot(fig, filename='custom-sized-subplot-with-subplot-titles')

newplot-4

@jonmmease jonmmease added the V4 label May 2, 2019
@gvwilson
Copy link
Contributor

Hi - we are currently trying to tidy up Plotly's public repositories to help us focus our efforts on things that will help users most. Since this issue has been sitting for several years, so I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our backlog. Thanks for your help - @gvwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

No branches or pull requests

3 participants