Santhosh
Santhosh

Reputation: 11834

plotly: not showing error when wrong data passed

I am just experimenting with plotly

df is a pandas dataframe with 10 columns

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=df[df.columns[0:4]],
        y=df[df.columns[0]],
        name="V1 Mag",
    ))

fig.show()

Here if i pass 4 columns (0,1,2,3) data to xaxis its accepting as x-axis without showing any error.

        x=df[df.columns[0:4]],
        y=df[df.columns[0]],

I am expecting that plotly should accpet only single array of data for each axis, else show error

Upvotes: 1

Views: 132

Answers (1)

Derek O
Derek O

Reputation: 19610

In my opinion it's a stylistic thing: some libraries are more strict and will throw an error, but others will simply have unexpected behavior if you don't use methods or parameters as intended. In this case, passing an array instead of a vector for the x coordinates will cause Plotly to try to best interpret what you meant.

import numpy as np
import pandas as pd
import plotly.graph_objects as go

np.random.seed(42)
df = pd.DataFrame(np.random.randint(0,100,size=(10, 10)))

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=df[df.columns[0:4]],
        y=df[df.columns[0]],
        name="V1 Mag",
    ))

fig.show()

enter image description here

And the DataFrame looks like this:

enter image description here

It looks like if you pass an m x n array where m > 1, Plotly will try to interpret the first two rows of the DataFrame as a category. In general, you will find that working with Plotly, it's a very flexible library, so it doesn't "break" that often, but it won't work as intended if you do something ambiguous like you're doing here.

Upvotes: 0

Related Questions