在 python 中过度绘制

Overplot in plotly in python

如何给图形添加线条?我试过figure.add_,但我不知道用什么来换行。当我颠倒顺序并使用figure.add_scatter时,我得到一个错误:

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()

当您调用 px.scatterpx.line 时,您将 DataFrame 和列名传递给参数 xy。当您调用 go.Scatter 时,您必须将 DataFrame 的列切片直接传递给 xy 参数。 add_scatter 函数是 performs the same function as adding a graph_object 的便捷方法,不是 plotly express 对象。因此,您必须向 add_scatter 传递与 go.Scatter.

相同的参数

例如,使用两个示例数据帧,以下代码有效:

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()

但是如果我用fig.add_scatter(df2, x='x', y='y')替换fig.add_scatter(x=df2['x'], y=df2['y']),我会得到一个错误:

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)

顺便说一句,我建议不要使用 import 语句 from plotly.graph_objs import *,因为它会从 plotly including ones you don't need.

中导入所有函数和 类