Plotly two yaxis

Hi, does anyone knows why this happens? Seens that my frist and my last point are connected …

this is the code i’m using:

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

df = pd.read_csv(‘data.csv’, index_col=0, parse_dates=True)

def plot_var_prev(dff):

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(
    go.Scatter(x=dff.index, 
            y=dff['mag_vento'].values,
            name="Magnitude"),
            secondary_y=False,
)

fig.add_trace(
    go.Scatter(x=dff.index, 
            y=dff['direcao_vento'].values, 
            name="Direção"),
            secondary_y=True,
)

fig.update_xaxes(title_text="<b>Data</b>")
fig.update_layout(hovermode="x", hoverlabel=dict(font_size=16, font_family="Poppins"))
fig.update_layout(plot_bgcolor='rgba(0, 0, 0, 0)', paper_bgcolor='rgba(0, 0, 0, 0)',)
fig.update_layout(
        legend=dict(orientation="v"),
        yaxis=dict(
            title=dict(text='<b>Magnitude do vento [m/s]</b>'),
            side="left",
            type='linear'
        ),
        yaxis2=dict(
            title=dict(text="<b>Direção do vento [°]</b>"),
            side="right",
            type='linear',
            overlaying="y",
            tickmode="sync",
            rangemode='tozero'
        ),
    )

return fig

The data and nootebook are avaiable here …

Hello, jp12

You have bad data in your data.csv file. I mean it has same rows twice. You can compare 2nd row (2023-07-19 00:00:00) and 67th row (2023-07-19 00:00:00) :wink:

Best solution is remove duplicates from a file. Another way is use pandas:

df = pd.read_csv('data.csv', parse_dates=True) # without index_col=0
df = df.drop_duplicates(subset=['dia'])

Change x=dff.index to x=dff[‘dia’] for solution with pandas

1 Like