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

plotly.express.plot() not working for multiple lines #4370

Closed
UtkarshVerma opened this issue Sep 26, 2023 · 2 comments
Closed

plotly.express.plot() not working for multiple lines #4370

UtkarshVerma opened this issue Sep 26, 2023 · 2 comments

Comments

@UtkarshVerma
Copy link

I have a project which has a snippet like this:

from enum import Enum
import pandas as pd
import plotly.express as px


class Column(Enum):
    Frequency = "f"
    Voltage = "V"
    Impedance = "Z"

    def __str__(self):
        if self is Column.Frequency:
            return "Frequency"
        elif self is Column.Voltage:
            return "Voltage"
        elif self is Column.Impedance:
            return "Impedance"
        else:
            raise ValueError("Invalid column")


df = pd.DataFrame(
    {
        Column.Frequency: range(10),
        Column.Voltage: range(10),
        Column.Impedance: range(10),
    }
)
display(df)
px.line(df, x=Column.Frequency, y=[Column.Voltage, Column.Impedance]).show()

I use enums as keys to my dataframe and plotly is able to plot if I pass a single column to plotly like y=Column.Impedance. However, when I pass it a list of columns, it shows the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb](https://file+.vscode-resource.vscode-cdn.net/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb) Cell 12 line 3
     [22](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=21) df = pd.DataFrame(
     [23](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=22)     {
     [24](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=23)         Column.Frequency: range(10),
   (...)
     [27](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=26)     }
     [28](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=27) )
     [29](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=28) display(df)
---> [30](vscode-notebook-cell:/home/subaru/projects/oral-cancer/analysis-v2/main.ipynb#X43sZmlsZQ%3D%3D?line=29) px.line(df, x=Column.Frequency, y=[Column.Voltage, Column.Impedance]).show()

File [~/projects/oral-cancer/analysis-v2/.venv/lib/python3.11/site-packages/plotly/express/_chart_types.py:264](https://file+.vscode-resource.vscode-cdn.net/home/subaru/projects/oral-cancer/analysis-v2/~/projects/oral-cancer/analysis-v2/.venv/lib/python3.11/site-packages/plotly/express/_chart_types.py:264), in line(data_frame, x, y, line_group, color, line_dash, symbol, hover_name, hover_data, custom_data, text, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, orientation, color_discrete_sequence, color_discrete_map, line_dash_sequence, line_dash_map, symbol_sequence, symbol_map, markers, log_x, log_y, range_x, range_y, line_shape, render_mode, title, template, width, height)
    216 def line(
    217     data_frame=None,
    218     x=None,
   (...)
    258     height=None,
    259 ) -> go.Figure:
    260     """
    261     In a 2D line plot, each row of `data_frame` is represented as vertex of
    262     a polyline mark in 2D space.
    263     """
--> 264     return make_figure(args=locals(), constructor=go.Scatter)
...
   1255         )
   1256     df_output[str(col_name)] = to_unindexed_series(argument, str(col_name))
   1258 # Finally, update argument with column name now that column exists

ValueError: All arguments should have the same length. The length of argument `y` is 2, whereas the length of  previously-processed arguments ['Frequency'] is 10

Please let me know how this could be addressed.

@UtkarshVerma
Copy link
Author

Note that this works if I use strings as dataframe keys instead of the enum.

@UtkarshVerma
Copy link
Author

I don't care anymore.

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

No branches or pull requests

1 participant