我写了代码
test ='test.csv'
test = pd.read_csv(test)
test1 =‘test1.csv'
test1 = pd.read_csv(test1)
测试显示
Date Score
2010-01-01 20
2010-01-02 30
2010-01-03 40
2010-01-04 50
test1显示
Date Score
2010-01-01 10
2010-01-03 40
2010-01-04 30
2010-01-10 60
我写了代码,
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import plotly
import plotly.graph_objs as go
import datetime
import plotly.offline as offline
plotly.offline.init_notebook_mode(connected=False)
test ='test.csv'
test = pd.read_csv(test)
test =test["Score"]
test1 ="test1.csv"
test1 = pd.read_csv(test1)
test1 =test1["Score"]
data = [
plotly.graph_objs.Scatter(y = test, mode = 'lines', name = 'TEST'),
plotly.graph_objs.Scatter(y = test1, mode = 'lines', name = 'TEST2', yaxis='y2'),
]
layout = plotly.graph_objs.Layout(
title="test",
xaxis={"title":"test"},
yaxis={"title":"test1"},
yaxis2={"title":"test2", "overlaying":"y", "side":"right"},
)
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.iplot(fig)
我要绘制test&test1数据在同一x轴上具有相同的日期。例如, 测试的2010年1月1日的20和测试1的2010年1月1日的10将在同一x轴上绘制。因此,如果由于缺乏数据而暂停日期,则只能绘制圆形,而绘制线形图。 我该怎么办?我的代码有什么问题?
答案 0 :(得分:1)
问题出在data
中,请检查以下各项以获取test
和test1
的具有双y轴的结果输出,如下所示:
test['Date'] = pd.to_datetime(test['Date'])
test
Date Score
0 2010-01-01 20
1 2010-01-02 30
2 2010-01-03 40
3 2010-01-04 50
test1['Date'] = pd.to_datetime(test1['Date'])
test1
Date Score
0 2010-01-01 10
1 2010-01-03 40
2 2010-01-04 30
3 2010-01-10 60
import plotly
import plotly.graph_objs as go
import datetime
import plotly.offline as offline
plotly.offline.init_notebook_mode(connected=False)
data = [go.Scatter(x = test.Date , y = test.Score , mode = 'lines', name = 'TEST'),
go.Scatter(x = test1.Date, y = test1.Score, mode = 'lines', name = 'TEST2',
yaxis='y2')]
layout = go.Layout(title="test",
xaxis={"title":"test"},
yaxis={"title":"test1"},
yaxis2={"title":"test2", "overlaying":"y", "side":"right"})
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.plot(fig)
图看起来像这样