plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.15k stars 2.54k forks source link

Updating layout of specific subplot #2483

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi there. I've spent the past few hours browsing the plotly documentation and stackoverflow to solve my problem, but I find nothing.

What I want to do, is update the layout of a specific subplot. If this is not possible, then updating all of them is fine as well.

I've tried -> https://plotly.com/python/subplots/ In the form of fig.update_xaxes(title_text="xaxis 1 title", row=1, col=1) But this did not work.

I've also tried to update the layout by trying to set xaxis2, as some Stack Overflow users suggested, this just gave me a key error. Maybe this was an old functionality?

From -> https://plotly.com/python/3d-axes/ I've tried: fig.update_layout(scene = dict( xaxis_title='X AXIS TITLE', yaxis_title='Y AXIS TITLE', zaxis_title='Z AXIS TITLE'), width=700, margin=dict(r=20, b=10, l=10, t=10))

This works, however it only updates the first subplot I have.

Essentially this is the code I have for adding the trace objects.

Screenshot_1

Then this is my attempt at editing the axes, yet this only updates the first plot.

Screenshot_2

This is my result, and it's really frustrating because I just want my axes labeled...

result

As you can probably see, the first plot is nicely zoomed out as well, however I have the exact same problem that when I update the layout, it only updates the first plot instead of all of them.

This is my make subplots code. While I'm glad that there's a subplot_titles argument, is there no functionality to do the same for the axes of the plots?

Screenshot_3

In another issue I saw one of the developers of Plotly reply that update_xaxes, and update_yaxes is the way to do this, but as I mentioned before this did not do anything for me. No errors, just the plots and no updated labels.

If anyone's knowledgeable in this, then thanks in advance.

nicolaskruchten commented 4 years ago

Ah, I'm sorry our documentation is giving you such a hard time! The problem here is that .update_xaxes() and .update_yaxes() operate on 2d charts, not 3d charts like yours. You'll have to use update_scenes which I now see isn't really anywhere in our docs, and is something we'll work on shortly (cc @emmanuelle @jdamiba)

I should add that the subplot "titles" themselves are actually annotations, stored in layout.annotations and not actually indexable by row/col unfortunately, so you have to set those correctly in the make_subplots() call otherwise they're pretty tricky to update.

ghost commented 4 years ago

Ah, I'm sorry our documentation is giving you such a hard time! The problem here is that .update_xaxes() and .update_yaxes() operate on 2d charts, not 3d charts like yours. You'll have to use update_scenes which I now see isn't really anywhere in our docs, and is something we'll work on shortly (cc @emmanuelle @jdamiba)

I should add that the subplot "titles" themselves are actually annotations, stored in layout.annotations and not actually indexable by row/col unfortunately, so you have to set those correctly in the make_subplots() call otherwise they're pretty tricky to update.

You know, the thought passed through my mind that it could've been only applicable to 2D plots (-:

In any case... I got it to work now. Thanks a bunch, Nicolas! Awesome of you to reply so quickly.

If anyone gets to this topic somehow with the same problem here's how I fixed it.

Screenshot_4

nicolaskruchten commented 4 years ago

OK, I'm glad you're unblocked! We'll get our docs updated to make this clearer 🙏

vontomar commented 2 years ago

OK, I'm glad you're unblocked! We'll get our docs updated to make this clearer 🙏

Hello Nicolas, I just read through the question "ghost" encountered with subplots regarding .update_xaxes() etc. Do you perhaps know how to access the axes of a Scatterpolar plot?

Many thanks in advance and best wishes, Martin

nicolaskruchten commented 2 years ago

@vontomar yes, you can use fig.update_polars() to update polar axes.

vontomar commented 2 years ago

@vontomar yes, you can use fig.update_polars() to update polar axes.

Thanks a lot Nicolas for your fast reply. I tried with fig.update_polars() which affects all Scatterpolar subplots in the widget. I am still trying to figure out how to address only one subplot? In my case, I just want to update the plot in row 3, column 4. I am currently trying this but I am not sure it will work:

polar34 = f.for_each_polar(f, row=3, col=4) polar34.update_layout(polar = dict(radialaxis = dict(angle=180, tickangle =-180, range = [0, 3]), angularaxis = dict(direction="clockwise", dtick = 30)))

If you have a hint I would be more than glad. Many thanks! Martin

nicolaskruchten commented 2 years ago

You can just do f.update_polars(..., row=3, col=4), there is no need to use for_each_polar.

vontomar commented 2 years ago

You can just do f.update_polars(..., row=3, col=4), there is no need to use for_each_polar.

Hi Nicolas Thank you so much, This works brilliantly. Best wishes, Martin