-
Notifications
You must be signed in to change notification settings - Fork 44
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
Feature request: support plotly backend for pandas plotting #249
Comments
what errors do you get? |
MWE: import pandas as pd
import pint_pandas as ppi
import matplotlib.pyplot as plt
pd.options.plotting.backend = "plotly"
ppi.PintType.ureg.setup_matplotlib()
df = pd.DataFrame({'A': pd.Series([1,2,3], dtype="pint[m]")})
df['A'].plot()
AttributeError: 'int' object has no attribute 'tolist' This works: df['A'].pint.magnitude.plot() Package versions (on Python 3.10):
|
AttributeError Traceback (most recent call last) AttributeError: 'float' object has no attribute 'tolist' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\basedatatypes.py:832, in BaseFigure.ipython_display(self) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_renderers.py:386, in show(fig, renderer, validate, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_renderers.py:294, in RenderersConfig._build_mime_bundle(self, fig_dict, renderers_string, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_base_renderers.py:578, in IFrameRenderer.to_mimebundle(self, fig_dict) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:505, in write_html(fig, file, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, validate, default_width, default_height, auto_open, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:142, in to_html(fig, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, default_width, default_height, validate, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_json.py:142, in to_json_plotly(plotly_object, pretty, engine) File ~\mambaforge\envs\pp_aug241\Lib\json_init_.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:56, in PlotlyJSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:200, in JSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:130, in PlotlyJSONEncoder.default(self, obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:147, in PlotlyJSONEncoder.encode_as_list(obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\pint\facets\plain\quantity.py:1435, in PlainQuantity.tolist(self) AttributeError: Magnitude 'float' does not support tolist.AttributeError Traceback (most recent call last) AttributeError: 'float' object has no attribute 'tolist' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\basedatatypes.py:823, in BaseFigure.repr_mimebundle(self, include, exclude, validate, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_renderers.py:294, in RenderersConfig._build_mime_bundle(self, fig_dict, renderers_string, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_base_renderers.py:578, in IFrameRenderer.to_mimebundle(self, fig_dict) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:505, in write_html(fig, file, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, validate, default_width, default_height, auto_open, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:142, in to_html(fig, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, default_width, default_height, validate, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_json.py:142, in to_json_plotly(plotly_object, pretty, engine) File ~\mambaforge\envs\pp_aug241\Lib\json_init_.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:56, in PlotlyJSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:200, in JSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:130, in PlotlyJSONEncoder.default(self, obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:147, in PlotlyJSONEncoder.encode_as_list(obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\pint\facets\plain\quantity.py:1435, in PlainQuantity.tolist(self) AttributeError: Magnitude 'float' does not support tolist.AttributeError Traceback (most recent call last) AttributeError: 'float' object has no attribute 'tolist' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\basedatatypes.py:806, in BaseFigure.repr_html(self) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\basedatatypes.py:823, in BaseFigure.repr_mimebundle(self, include, exclude, validate, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_renderers.py:294, in RenderersConfig._build_mime_bundle(self, fig_dict, renderers_string, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_base_renderers.py:578, in IFrameRenderer.to_mimebundle(self, fig_dict) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:505, in write_html(fig, file, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, validate, default_width, default_height, auto_open, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:142, in to_html(fig, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, default_width, default_height, validate, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_json.py:142, in to_json_plotly(plotly_object, pretty, engine) File ~\mambaforge\envs\pp_aug241\Lib\json_init_.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:56, in PlotlyJSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:200, in JSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:130, in PlotlyJSONEncoder.default(self, obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:147, in PlotlyJSONEncoder.encode_as_list(obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\pint\facets\plain\quantity.py:1435, in PlainQuantity.tolist(self) AttributeError: Magnitude 'float' does not support tolist. Figure({
'data': [{'hovertemplate': 'variable=A<br>index=%{x}<br>value=%{y}<extra></extra>',
'legendgroup': 'A',
'line': {'color': '#636efa', 'dash': 'solid'},
'marker': {'symbol': 'circle'},
'mode': 'lines',
'name': 'A',
'orientation': 'v',
'showlegend': True,
'type': 'scatter',
'x': array([0, 1, 2]),
'xaxis': 'x',
'y': array([<Quantity(1.0, 'meter')>, <Quantity(2.0, 'meter')>,
<Quantity(3.0, 'meter')>], dtype=object),
'yaxis': 'y'}],
'layout': {'legend': {'title': {'text': 'variable'}, 'tracegroupgap': 0},
'margin': {'t': 60},
'template': '...',
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'index'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'value'}}}
}) I note that it's showing the ydata as an array of qty
|
I get the same tolist error when trying to plot an array of qty: import plotly.express as px
import pint
import numpy as np
Q_ = pint.Quantity
fig = px.line(x=["a","b","c"], y=np.array([Q_(1,"m"),Q_(1,"m"),Q_(1,"m")],dtype="object"), title="sample figure")
print(fig)
fig.show()
Figure({
'data': [{'hovertemplate': 'x=%{x}<br>y=%{y}<extra></extra>',
'legendgroup': '',
'line': {'color': '#636efa', 'dash': 'solid'},
'marker': {'symbol': 'circle'},
'mode': 'lines',
'name': '',
'orientation': 'v',
'showlegend': False,
'type': 'scatter',
'x': array(['a', 'b', 'c'], dtype=object),
'xaxis': 'x',
'y': array([<Quantity(1, 'meter')>, <Quantity(1, 'meter')>, <Quantity(1, 'meter')>],
dtype=object),
'yaxis': 'y'}],
'layout': {'legend': {'tracegroupgap': 0},
'template': '...',
'title': {'text': 'sample figure'},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'x'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'y'}}}
})
```python3
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
File ~\mambaforge\envs\pp_aug241\Lib\site-packages\pint\facets\plain\quantity.py:1424, in PlainQuantity.tolist(self)
1423 try:
-> 1424 values = self._magnitude.tolist()
1425 if not isinstance(values, list):
AttributeError: 'int' object has no attribute 'tolist' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\basedatatypes.py:3410, in BaseFigure.show(self, *args, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_renderers.py:386, in show(fig, renderer, validate, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_renderers.py:294, in RenderersConfig._build_mime_bundle(self, fig_dict, renderers_string, **kwargs) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_base_renderers.py:578, in IFrameRenderer.to_mimebundle(self, fig_dict) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:505, in write_html(fig, file, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, validate, default_width, default_height, auto_open, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_html.py:142, in to_html(fig, config, auto_play, include_plotlyjs, include_mathjax, post_script, full_html, animation_opts, default_width, default_height, validate, div_id) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\plotly\io_json.py:142, in to_json_plotly(plotly_object, pretty, engine) File ~\mambaforge\envs\pp_aug241\Lib\json_init_.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:56, in PlotlyJSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:200, in JSONEncoder.encode(self, o) File ~\mambaforge\envs\pp_aug241\Lib\json\encoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:130, in PlotlyJSONEncoder.default(self, obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages_plotly_utils\utils.py:147, in PlotlyJSONEncoder.encode_as_list(obj) File ~\mambaforge\envs\pp_aug241\Lib\site-packages\pint\facets\plain\quantity.py:1435, in PlainQuantity.tolist(self) AttributeError: Magnitude 'int' does not support tolist.
|
this looks like plotly is not recognising pint scalars as scalars, so tries to convert them to list. I'd try opening an issue in plotly with my last post as the MWE |
This package is amazing. I have been struggling to use the plotly backend for pandas plots when my dataframe has units associated with it. I have had to strip out the units to get the plotting to work. Is there a way to plot a pint_dataframe with plotly?
I'm happy to provide a MWE if that helps.
Thanks.
The text was updated successfully, but these errors were encountered: