Open hdavid16 opened 3 months ago
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):
'pandas>=2.2.2',
'numpy>=2.0.1',
'Pint>=0.24.3',
'Pint-Pandas>=0.6.2',
'matplotlib>=3.9.2',
'plotly>=5.23.0',
'nbformat>=4.2.0'
Output:
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
'y': array([<Quantity(1.0, 'meter')>, <Quantity(2.0, 'meter')>,
<Quantity(3.0, 'meter')>], dtype=object),
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'}}}
})
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.