图表中线和散点的混合

时间:2020-04-22 16:26:33

标签: python plotly

使用此代码:

trace = go.Scatter(x=[1,2,3],
                    y=[1,2,3],
                    name = "chart1",
                    mode="markers+text",
                    opacity = 1)

layout = dict(width=800,
    height=600,autosize=True,margin=dict(
        l=50,
        r=50,
        b=100,
        t=100,
        pad=4
    ))

iplot(dict(data=[trace], layout=layout))

渲染器:

enter image description here

如何在此图表中包含一条包含对应于x值[[3,1,4]的y值1,2,3]的线?

此代码:

# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Data
df=pd.DataFrame({'x': range(1,11), 'y1': np.random.randn(10), 'y2': np.random.randn(10)+range(1,11), 'y3': np.random.randn(10)+range(11,21) })

plt.plot(('x', 'y1'), data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot(('x', 'y2'), data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot(('x', 'y3'), data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4, linestyle='dashed', label="toto")

plt.legend()

达到类似的效果,但缺少情节样式:

enter image description here

我还尝试在散点图的创建中包括y值:

y=[[1,2,3] , [3,1,4]]

但是图表未按预期呈现:

enter image description here

2 个答案:

答案 0 :(得分:1)

添加另一条痕迹:

trace = go.Scatter(...)  # your original trace
trace2 = go.Scatter(x=[1,2,3], y=[3,1,4], name='chart2')

layout = ...

iplot(dict(data=[trace, trace2], layout=layout))

enter image description here

答案 1 :(得分:1)

您也可以这样做:

fig = go.Figure()
fig.add_Trace(your trace)
fig.add_trace(go.Scatter(x=[1,2,3], y=[3,1,4],
                mode='lines',
                name='lines'))
fig.add_layout(dict(width=800,
    height=600,autosize=True,margin=dict(
        l=50,r=50,b=100,t=100, pad=4))
fig.show()
相关问题