0

I wanted to create an interactive plot with matplotlib in google colab. It seems like a complex task so I want a little help to convert this piece of code which is in matplotlib to Plotly.

close = df['A']
fig = plt.figure(figsize = (15,5))
plt.plot(close, color='r', lw=2.)
plt.plot(close, '^', markersize=10, color='m', label = 'signal X', markevery = df_x)
plt.plot(close, 'v', markersize=10, color='k', label = 'signal Y', markevery = df_y)
plt.title('Turtle Agent: total gains %f, total investment %f%%'%(df_A, df_B))
plt.legend()
plt.show()

enter image description here

2
  • 1
    can you provide your data as a link or at a minimum sample data in question Commented Oct 28, 2021 at 11:08
  • df['A'] is a pandas.core.series.Series and sample values are : 89.71 89.94 90.06; df_x and df_y are lists. e,g, df_x = [0,12,0,23,0,34,0,21...] df_y = [12,434,54,2,6,76...] df_A and df_B are numpy.float64 e.g. df_A = -3.2325352343... df_B = -23.35454.... This is it basically. @RobRaymond Commented Oct 28, 2021 at 11:29

1 Answer 1

3
  • using sample data from plotly OHLC examples https://plotly.com/python/ohlc-charts/
    1. create a line trace
    2. add scatter traces based on filters of data frame with required formatting. This is done as a list comprehension, could be done as inline code
import pandas as pd
import numpy as np
import plotly.express as px

df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv"
)
df["Date"] = pd.to_datetime(df["Date"])
# make data set more useful for demonstrating this plot
df.loc[df.sample((len(df)//8)*7).index, "direction"] = np.nan

px.line(df, x="Date", y="AAPL.Close").update_traces(line_color="red").add_traces(
    [
        px.scatter(
            df.loc[df["direction"].eq(filter)], x="Date", y="AAPL.Close"
        )
        .update_traces(marker=fmt)
        .data[0]
        for filter, fmt in zip(
            ["Increasing", "Decreasing"],
            [
                {"color": "black", "symbol": "triangle-up", "size": 10},
                {"color": "blue", "symbol": "triangle-down", "size": 10},
            ],
        )
    ]
)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.