keerthan kumar
keerthan kumar

Reputation: 391

python-plotly multiple lines in same graph with same Y axis

I have a csv file that looks like this:

time,price,m1,m2,m3,m4,m5,m6,m7,m8,buy/sell

10.30.01,102,105,100.5,103.5,110,100.9,103.02,111,105.0204,
10.30.02,103,104.5,101,104,110.2,101.4,104.03,110.5,104.5204,
10.30.03,104,104,101.5,104.5,110.4,101.9,105.04,110,104.0204,
10.30.04,105,103.5,102,105,110.6,102.4,106.05,109.5,103.5204,
10.30.05,106,103,102.5,105.5,110.8,102.9,107.06,109,103.0204,
10.30.06,107,102.5,103,106,111,103.4,108.07,108.5,102.5204,
10.30.07,108,102,103.5,106.5,111.2,103.9,109.08,108,102.0204,
10.30.08,109,101.5,104,107,111.4,104.4,110.09,107.5,101.5204,BUY
10.30.09,110,101,104.5,107.5,111.6,104.9,111.1,107,101.0204,
10.30.10,111,100.5,105,108,111.8,105.4,112.11,106.5,100.5204,
10.30.11,112,101,105.5,108.5,112,105.9,113.12,106,101.0204,
10.30.12,113,101.5,106,109,112.2,106.4,114.13,105.5,101.5204,SELL
10.30.13,114,102,106.5,109.5,112.4,106.9,115.14,105,102.0204,
10.30.14,115,102.5,107,110,112.6,107.4,116.15,104.5,102.5204,
10.30.15,116,103,107.5,110.5,112.8,107.9,117.16,104,103.0204,BUY
10.30.16,117,103.5,108,111,113,108.4,118.17,103.5,103.5204,

I want to take time in x-axis and price,m1,m2,m3,m4,m5,m6,m7,m8 in Y axis, since its the same range all are in same y-axis as line graphs. and buy/sell column in the same graph as scatter plot. How to do this with plotly ?

sorry for the simple question (if it is one), I tried a lot couldn't crack it. thank you in advance

Upvotes: 1

Views: 581

Answers (1)

Derek O
Derek O

Reputation: 19565

A great resource for Scatter plot related questions is Plotly's documentation on scatter plots.

Plotting all of the columns price,m1,m2,m3,m4,m5,m6,m7,m8 can be done by looping through a list, and adding each of these columns as a trace.

Then I would recommend that you draw vertical lines in the Scatter plot for each time with BUY or SELL, by iterating through the non-null entries in the buy/sell column and using a shape to create a vertical line. You can also add an arrow and text pointing to each line using an annotation.

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.read_csv("buysell.csv")

fig = go.Figure()
cols = ['price','m1','m2','m3','m4','m5','m6','m7','m8']

for col in cols:
    fig.add_trace(go.Scatter(
        x=df['time'],
        y=df[col],
        name=col
    ))

# iterate over any rows with 'BUY' or 'SELL'
for index, row in df.dropna(subset=['buy/sell']).iterrows():
    fig.add_shape(
        type='line',
        x0=row['time'],
        y0=0,
        x1=row['time'],
        y1=1,
        yref='paper',
        line=dict(
            color="red",
            width=1,
            dash="dot",
        )
    )
    df_max, df_min = df[cols].max().max(), df[cols].min().min()
    fig.add_annotation(
        x=row['time'], 
        y=df_max,
        text=row['buy/sell'],
            showarrow=True,
            arrowhead=4,
            )

fig.show()

enter image description here

Upvotes: 1

Related Questions