Skip to content

mplexporter not getting Collection offset position due to matplotlib API changes in v3.5 #3624

Description

@cylammarco

Using plotly 5.6.0 and matplotlib 3.5.1

https://matplotlib.org/devdocs/api/prev_api_changes/api_changes_3.5.0.html#classes-methods-and-attributes

Collection.set_offset_position and Collection.get_offset_position have been removed; the offset_position of the Collection class is now "screen"

Example code to reproduce the problem:

import numpy as np
from matplotlib import pyplot as plt
from plotly import tools

x = np.arange(10)
y = x **2.

fig1 = plt.figure(1)
plt.vlines(x, 0, max(y))

tools.mpl_to_plotly(fig1)

gives

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\------\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\tools.py", line 112, in mpl_to_plotly
    matplotlylib.Exporter(renderer).run(fig)
  File "C:\Users\------\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 53, in run
    self.crawl_fig(fig)
  File "C:\Users\------\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 124, in crawl_fig
    self.crawl_ax(ax)
  File "C:\Users\------\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 146, in crawl_ax
    self.draw_collection(ax, collection)
  File "C:\Users\------\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 289, in draw_collection
    offset_order = offset_dict[collection.get_offset_position()]
AttributeError: 'LineCollection' object has no attribute 'get_offset_position'. Did you mean: '_offset_position'?

Activity

  1. alberttorosyan commented on Mar 15, 2022

    @alberttorosyan

    facing similar issue with

    plotly==5.4.0
    matplotlib==3.5.0
    

    it seems the reason is plolty uses deprecated API which was dropped in mpl 3.5.0:
    https://matplotlib.org/3.5.1/api/prev_api_changes/api_changes_3.5.0.html#removals

  2. alberttorosyan commented on Mar 15, 2022

    @alberttorosyan

    @cylammarco are you waiting for the fix or you managed to find some sort of workaround here?

  3. cylammarco commented on Mar 15, 2022

    @cylammarco
    Author

    @alberttorosyan Speaking out loud, it should be a simple fix that requires changes in a few lines at:

    offset_order = offset_dict[collection.get_offset_position()]
    self.renderer.draw_path_collection(
    paths=processed_paths,
    path_coordinates=path_coords,
    path_transforms=path_transforms,
    offsets=offsets,
    offset_coordinates=offset_coords,
    offset_order=offset_order,
    styles=styles,
    mplobj=collection,
    )

    However, Plotly doesn't have a development/contribution guide, I don't know how they would like to handle this kind of deprecation. any thoughts?

  4. alberttorosyan commented on Mar 15, 2022

    @alberttorosyan

    @cylammarco thinking the same here...
    It is possible to apply runtime patch until this gets fixed, but I don't love much the idea of using unittest module in production code

  5. cylammarco commented on Mar 23, 2022

    @cylammarco
    Author

    seems like it isn't as straightforward as I thought:

    plotly.py\packages\python\plotly\plotly\matplotlylib\renderer.py:574: UserWarning:
    
    Dang! That path collection is out of this world. I totally don't know what to do with it yet! Plotly can only import path collections linked to 'data' coordinates
    

    the mplexporter cannot grab the 'data' post-v3.5

  6. alberttorosyan commented on Mar 24, 2022

    @alberttorosyan

    we finally moved away from this.
    as an alternative, converting matplotlib figures to image is used in places where interactions (zoom in/out etc.) are not critical.

  7. SetsunaAng commented on Jun 21, 2022

    @SetsunaAng

    @alberttorosyan Speaking out loud, it should be a simple fix that requires changes in a few lines at:

    offset_order = offset_dict[collection.get_offset_position()]
    self.renderer.draw_path_collection(
    paths=processed_paths,
    path_coordinates=path_coords,
    path_transforms=path_transforms,
    offsets=offsets,
    offset_coordinates=offset_coords,
    offset_order=offset_order,
    styles=styles,
    mplobj=collection,
    )

    However, Plotly doesn't have a development/contribution guide, I don't know how they would like to handle this kind of deprecation. any thoughts?

    I'm not using Plotly directly but mplleaflet and I got the same error. I managed to make it work by replacing in this exporter.py file
    offset_order = offset_dict[collection.get_offset_position()]
    by
    offset_order = offset_dict[collection._offset_position]

    Hope it helps someone!

  8. letainc commented on Jun 22, 2022

    @letainc

    @alberttorosyan Speaking out loud, it should be a simple fix that requires changes in a few lines at:

    offset_order = offset_dict[collection.get_offset_position()]
    self.renderer.draw_path_collection(
    paths=processed_paths,
    path_coordinates=path_coords,
    path_transforms=path_transforms,
    offsets=offsets,
    offset_coordinates=offset_coords,
    offset_order=offset_order,
    styles=styles,
    mplobj=collection,
    )

    However, Plotly doesn't have a development/contribution guide, I don't know how they would like to handle this kind of deprecation. any thoughts?

    I'm not using Plotly directly but mplleaflet and I got the same error. I managed to make it work by replacing in this exporter.py file offset_order = offset_dict[collection.get_offset_position()] by offset_order = offset_dict[collection._offset_position]

    Hope it helps someone!

    I tried it. Expected renko plot however I only got the bar chart.
    fig, ax = mpl.plot(data, type="renko", style='yahoo', figsize=(15,10), returnfig=True, volume=True)
    plotly_fig = tls.mpl_to_plotly(fig)
    st.plotly_chart(plotly_fig, use_container_width=True)
    image

  9. SiddhantSadangi commented on Dec 28, 2022

    @SiddhantSadangi

    any update here? Are we stuck with using matplotlib 3.4?

  10. cylammarco commented on Jan 3, 2023

    @cylammarco
    Author

    @SiddhantSadangi I have not done anything against this issue :|

    #3624 (comment) is probably the best solution at the moment(?).

  11. SiddhantSadangi commented on Jan 3, 2023

    @SiddhantSadangi

    #3624 (comment) is probably the best solution at the moment(?).

    This unfortunately won't work in our case as we use github actions :(

  12. NeilGirdhar commented on Oct 3, 2023

    @NeilGirdhar
    Contributor

    Is there a way that I can help? This issue is now a year and a half old. This behavior breaks Weights and Balances's use of Matplotlib.

  13. alexcjohnson commented on Oct 4, 2023

    @alexcjohnson
    Collaborator

    Fixed in #4372 (thanks @NeilGirdhar 🙇 ), I expect we'll release next week with the next plotly.js minor.

  14. mfouesneau commented on May 21, 2024

    @mfouesneau

    It's still happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions