3

I'm trying to plot the daily price and the total number of bid and ask orders per day for a typical stock on the same figure.

Here is what I've done so far, but I think there is a more efficient way to create the same plot using Plotly.

import pandas as pd
import plotly.graph_objects as go

# Create the base figure
fig = go.Figure()
# Add a line graph
fig.add_trace(
    go.Scatter(x=df.index, y=df['price'], name='Daily Price'))

# Create financial buttons
fin_buttons = [
    {'count': 7, 'label': "1WTD", 'step': "day", 'stepmode': "todate"},
    dict(count=7, label="1W", step="day", stepmode="backward"),
    {'count': 1, 'label': "1MTD", 'step': "month", 'stepmode': "todate"},
    dict(count=1, label="1M", step="month", stepmode="backward"),
    {'count': 3, 'label': "3MTD", 'step': "month", 'stepmode': "todate"},
    dict(count=3, label="3M", step="month", stepmode="backward"),
    {'count': 6, 'label': "6MTD", 'step': "month", 'stepmode': "todate"},
    dict(count=6, label="6M", step="month", stepmode="backward"),
    {'count': 1, 'label': "YTD", 'step': "year", 'stepmode': "todate"},
    {'count': 1, 'label': "1Y", 'step': "year", 'stepmode': "backward"},
    dict(step="all")
]
# Create the date range buttons & show the plot
fig.update_layout({'xaxis': {'rangeselector': {'buttons': fin_buttons}}})
fig.update_layout(yaxis=dict(autorange=True))
# Add bar chart
fig.add_trace(
    go.Bar(name='Bids',
           x=df.index,
           y=df['bid']))
# Add bar chart
fig.add_trace(
    go.Bar(name='Asks',
           x=df.index,
           y=-df['ask']))
# Show the plot
fig.show()

The problem is autorange property does not rescale the y-axis. In other words, when I click a time button, the y-axis for the price line chart does not automatically reset!

enter image description here

enter image description here

Flavia Giammarino
  • 7,987
  • 11
  • 30
  • 40
sci9
  • 700
  • 1
  • 7
  • 21
  • It is said that the y-axis scale does not change when the range selector is selected, but it is the x-axis that is changing, not the y-axis value. What does it mean that the scale does not change? Also, what is an efficient code, and what is a concrete example? Creating a button? – r-beginners Aug 10 '21 at 13:47
  • @r-beginners Thank you for your comment. 1) I mean how do I force the y-axis to be adjusted based on the portion of the data in view? 2) By efficient code, I mean how does a Plotly expert visualize this task? Is theere another way to regenerate the same figure? – sci9 Aug 10 '21 at 14:08

0 Answers0