1

How to add a line to a figure? I tried figure.add_, but I do not know what to use for a line. When I reversed the order and used figure.add_scatter, I obtained an error:

The 'cliponaxis' property must be specified as a bool (either True, or False)

import plotly.express as px
import numpy as np
import pandas as pd
from plotly.graph_objs import *
import plotly.graph_objects as go

data_n = np.loadtxt('f.dat', unpack=True, usecols=[0, 2, 5, 7])

data = data_n.tolist()
d = {'A': data[0], 'B': data[1]}

fig = px.scatter(df, x='A', y='B')


x_new = ...
y_new = ...

d_fit = {'x': x_new, 'y': y_new}
df_fit = pd.DataFrame(data=d_fit)

fig = px.line(df_fit, x='x', y='y') # to add

fig.show()
Derek O
  • 16,770
  • 4
  • 24
  • 43
Elena Greg
  • 1,061
  • 1
  • 11
  • 26

1 Answers1

2

When you call px.scatter or px.line, you pass the DataFrame, and the names of the columns to the parameters x and y. When you call go.Scatter you have to pass column slice sof the DataFrame directly to the x and y parameters. The add_scatter function is a convenience method that performs the same function as adding a graph_object, NOT a plotly express object. Therefore, you have to pass the same parameters to add_scatter as you would to go.Scatter.

For example, using two sample DataFrames, the following code works:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
df2=pd.DataFrame({'x':[1,2,3],'y':[7,8,9]})

fig = px.scatter(df1, x='A', y='B')
fig.add_scatter(x=df2['x'], y=df2['y'])
fig.show()

enter image description here

But if I replace fig.add_scatter(x=df2['x'], y=df2['y']) with fig.add_scatter(df2, x='x', y='y'), I will get an error:

ValueError: 
    Invalid value of type 'pandas.core.frame.DataFrame' received for the 'cliponaxis' property of scatter
        Received value:    x  y
0  1  7
1  2  8
2  3  9

The 'cliponaxis' property must be specified as a bool
(either True, or False)

By the way, I would recommend not using the import statement from plotly.graph_objs import * as this imports all of the functions and classes from plotly including ones you don't need.

Derek O
  • 16,770
  • 4
  • 24
  • 43
  • Why it is not possible to add name = 'A' to fig = px.scatter(df1, x='A', y='B'), please? – Elena Greg May 14 '21 at 14:05
  • 1
    The parameter `name` is only valid for graph_objects so something like `fig = go.Figure(go.Scatter(x=df1['A'], y=df1['B'], name='A'))` would work – Derek O May 14 '21 at 15:55
  • Thank you and px... does not work? Name in fig.add_scatter() works well. With go, fig.update_traces() does not work. – Elena Greg May 14 '21 at 15:58
  • 1
    If you want to add another trace, I think you should use `fig.add_trace(...)` – Derek O May 14 '21 at 16:07
  • Is there an argument other than the name to write a name in a legend for px? With go, it is necessary to change lot of commands. – Elena Greg May 14 '21 at 16:21
  • 1
    I think you can actually try: `fig.update_traces(name='A', showlegend = True)` after `fig = px.scatter(...)` – Derek O May 14 '21 at 16:29