0

I am trying to analyse previous market corrections and found a dataset with a date format year.month (ex: 2021.11 for this month) and other data including the price at the given date. I imported it via pandas and made it into a pandas dataframe, subsetting the date and the price.

When I use the code below to plot the data I get a result where the plot goes up horinzontally with random indentations where there shouldn´t be one.

import plotly.graph_objects as go
import pandas as pd
fig = go.Figure([go.Scatter(x=analysedata['Date'], y=analysedata['P'])])
fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month",                                        
                 stepmode="backward"),
            dict(count=6, label="6m", step="month",  
                 stepmode="backward"),
            dict(count=1, label="YTD", step="year", 
                 stepmode="todate"),
            dict(count=1, label="1y", step="year", 
                 stepmode="backward"),
            dict(step="all")
        ])
    )
)
fig.show()

enter image description here

enter image description here

micro5
  • 415
  • 3
  • 6
Luk
  • 19
  • 2
  • Try converting you date column to datetime: `analysedata['Date'] = pd.to_datetime(analysedata['Date'].astype(str), format='%Y.%m')` – It_is_Chris Dec 08 '21 at 21:33
  • It changes the chart slightly but the problem remains the same unfortunately. I appreciate the help. – Luk Dec 08 '21 at 23:48

1 Answers1

0

The issue was the data type of the performance column in the pandas dataframe

The solution is to change the datatype to a numeric value in this manner:

import plotly.graph_objects as go
analysedata = data[['Date','P']]  
analysedata.set_index(['Date'], drop = False ,inplace = True)

pd.to_datetime(analysedata['Date'].astype(str), format='%Y.%m')
new = pd.to_numeric(analysedata['P'])

fig = go.Figure([go.Scatter(x=analysedata['Date'], y=new)])
fig.show()

Please be careful about using the "new =", otherwise when plotting it will come back to and see the values with the wrong data type.

Plotting resuls: Market since 1800s

Mr. T
  • 11,960
  • 10
  • 32
  • 54
Luk
  • 19
  • 2