我想在colab中使用Plotly绘制来自两个不同csv文件(不过列名相同(的数据。绘制我当前拥有的一个csv:
import pandas as pd
import plotly.express as px
a1 = ('/content/a1.csv')
a2 = ('/content/a2.csv')
b1 = ('/content/b1.csv')
b2 = ('/content/b2.csv')
a1_df = pd.read_csv(a1)
a1_graph = px.line(a1_df, x = 'timestamp', y = 'raw_1', title='a1')
a1_graph.show()
但是,有可能在同一个px.line函数中绘制两个csv吗?
您可以将数据帧连接在一起,并使用颜色kwarg分隔行。
import pandas as pd
import plotly.express as px
a1 = ('/content/a1.csv')
a2 = ('/content/a2.csv')
b1 = ('/content/b1.csv')
b2 = ('/content/b2.csv')
dfList = []
a1_df = pd.read_csv(a1)
a1_df['file'] = 'a1'
dfList.append(a1_df)
a2_df = pd.read_csv(a2)
a2_df['file'] = 'a2'
dfList.append(a2_df)
b1_df = pd.read_csv(b1)
b1_df['file'] = 'b1'
dfList.append(b1_df)
b2_df = pd.read_csv(b2)
b2_df['file'] = 'b2'
dfList.append(b2_df)
df = pd.concat(dfList, axis=1)
a1_graph = px.line(df, x = 'timestamp', y = 'raw_1', color = 'file', title='all files')
a1_graph.show()