Python 我想在两个日期的同一点上绘制数据
我写代码Python 我想在两个日期的同一点上绘制数据,python,pandas,plotly,Python,Pandas,Plotly,我写代码 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 测试1显示 Date
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
测试1显示
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-01-01的20和测试的2010-01-01的10将绘制在同一个x轴上。因此,如果由于缺少数据而暂停日期,则可以只绘制圆图,而不绘制线图。
我该怎么做?我的代码中有什么错误?问题在于数据,请检查以下内容,以获得测试和测试1的双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)
图表如下所示
阅读这个文档页面@AkashBadam Iread。但是我找不到应该如何编写代码来完成我的理想任务。理想的任务是什么?