Skip to content

tickmode="array" with custom tickvals ticktext doesn't work at all #2478

Closed

Description

@eromoe

From plotly doc:

enter image description here

Example:

import pandas as pd
import numpy as np

np.random.seed(42)
feature = pd.DataFrame({'ds': pd.date_range('20200101', periods=100*24, freq='H'), 
                        'y': np.random.randint(0,20, 100*24) , 
                        'yhat': np.random.randint(0,20, 100*24) , 
                        'price': np.random.choice([6600, 7000, 5500, 7800], 100*24)})


import plotly.graph_objects as go
import plotly.offline as py
import plotly.express as px
from plotly.offline import init_notebook_mode

init_notebook_mode(connected=True)


y = feature.set_index('ds').resample('D')['y'].sum()

fig = go.Figure()
fig.add_trace(go.Scatter(x=y.index, y=y))


x_dates = y.index.to_series().dt.strftime('%Y-%m-%d').sort_values().unique()


layout = dict(
    xaxis=dict(
        tickmode="array",
        tickvals=np.arange(0, x_dates.shape[0],2).astype(int),
        ticktext=x_dates[::2],
        tickformat='%Y-%m-%d',
        tickangle=45,
    )
)

fig.update_layout(layout)
fig.show()

Result:

enter image description here

Since length of x_dates[::2] is 50 , the ticknumber doesn't match at all .
How do I sovle it ??

Activity

  1. nicolaskruchten commented on May 19, 2020

    @nicolaskruchten
    Contributor

    Your layout = dict( ... line should start with fig.layout = dict( ... :)

  2. eromoe commented on May 20, 2020

    @eromoe
    Author

    @nicolaskruchten Sorry , lost a line when editing , add fig.update_layout(layout) now .

    You could try my code to verify the problem .

    Only feed integer values to x and tickvals can get right xticks . I didn't tried this becasue similar code worked in matplotlib. So it looks like plotly has some problems when handling dtype { 'x' : date, tickvals: int } and dtype { 'x' : date, tickvals: date } .

  3. nicolaskruchten commented on May 20, 2020

    @nicolaskruchten
    Contributor

    OK, so I'm not sure what the problem is here... what are you expecting to see that you're not seeing?

  4. nicolaskruchten commented on May 20, 2020

    @nicolaskruchten
    Contributor

    Let's look at a simpler example with fewer moving parts which seems to work well enough... Is this as you're expecting?

    import plotly.graph_objects as go
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=["2010-01-01", "2010-02-01", "2010-03-01"], y=[1,3,2]))
    fig.update_xaxes(tickvals=["2010-01-15", "2010-02-15"], ticktext=["A","B"])
    fig.show()

    image

  5. eromoe commented on May 20, 2020

    @eromoe
    Author

    Similar, on my example . x is date type , tickvals is int and ticktext is string , working in matplotlib . I also tried
    1 . x date , tickvals date ticktext string
    2. x string, tickvals string , ticktext string

    Neither work, so I post this issue .

  6. nicolaskruchten commented on May 20, 2020

    @nicolaskruchten
    Contributor

    I'm really sorry but I'm having a lot of trouble understanding the issue you're describing when you say "neither work"... In the example I've posted above, x, tickvals and ticktext are all strings, so it's case 2 you describe above and the output is as I would expect it. The behaviour is the same if x and tickvals are dates.

    If you could tell me what "doesn't work" about the display above I would gladly help you, but I'm not familiar enough with what matplotlib does to be able to infer this from what you've written :)

  7. eromoe commented on May 20, 2020

    @eromoe
    Author

    OK, I put full example here :

    I like to see

    import pandas as pd
    import numpy as np
    
    np.random.seed(42)
    feature = pd.DataFrame({'ds': pd.date_range('20200101', periods=100*24, freq='H'), 
                            'y': np.random.randint(0,20, 100*24) , 
                            'yhat': np.random.randint(0,20, 100*24) , 
                            'price': np.random.choice([6600, 7000, 5500, 7800], 100*24)})
    
    
    import plotly.graph_objects as go
    import plotly.offline as py
    import plotly.express as px
    from plotly.offline import init_notebook_mode
    
    init_notebook_mode(connected=True)
    
    
    y = feature.set_index('ds').resample('D')['y'].sum()
    
    fig = go.Figure()
    
    x_dates = y.index.to_series().dt.strftime('%Y-%m-%d').sort_values().unique()
    fig.add_trace(go.Scatter(x=x_dates, y=y))
    
    layout = dict(
        xaxis=dict(
            tickmode="auto",
            nticks=50,
            tickformat='%Y-%m-%d',
            tickangle=45,
        )
    )
    
    fig.update_layout(layout)
    fig.show()
    
    

    image


    tickmode="auto" and nticks=50 is fine for me .

    I am here just want to report this problem , because plotly is very handy now, I hope it would be bettter

    1. x string, tickvals string , ticktext string , doesn't show 50 xticks :
    import pandas as pd
    import numpy as np
    
    np.random.seed(42)
    feature = pd.DataFrame({'ds': pd.date_range('20200101', periods=100*24, freq='H'), 
                            'y': np.random.randint(0,20, 100*24) , 
                            'yhat': np.random.randint(0,20, 100*24) , 
                            'price': np.random.choice([6600, 7000, 5500, 7800], 100*24)})
    
    
    import plotly.graph_objects as go
    import plotly.offline as py
    import plotly.express as px
    from plotly.offline import init_notebook_mode
    
    init_notebook_mode(connected=True)
    
    
    y = feature.set_index('ds').resample('D')['y'].sum()
    
    fig = go.Figure()
    
    x_dates = y.index.to_series().dt.strftime('%Y-%m-%d').sort_values().unique()
    fig.add_trace(go.Scatter(x=x_dates, y=y))    # x is string
    
    layout = dict(
        xaxis=dict(
            tickmode="array",
            tickvals=x_dates[::2],    # string
            ticktext=x_dates[::2],    # string 
            tickformat='%Y-%m-%d',
            tickangle=45,
        )
    )
    
    fig.update_layout(layout)
    fig.show()
    
    

    results with wrong xticks :
    image

    2 . x date , tickvals date ticktext string

    import pandas as pd
    import numpy as np
    
    np.random.seed(42)
    feature = pd.DataFrame({'ds': pd.date_range('20200101', periods=100*24, freq='H'), 
                            'y': np.random.randint(0,20, 100*24) , 
                            'yhat': np.random.randint(0,20, 100*24) , 
                            'price': np.random.choice([6600, 7000, 5500, 7800], 100*24)})
    
    
    import plotly.graph_objects as go
    import plotly.offline as py
    import plotly.express as px
    from plotly.offline import init_notebook_mode
    
    init_notebook_mode(connected=True)
    
    
    y = feature.set_index('ds').resample('D')['y'].sum()
    
    fig = go.Figure()
    
    x_dates = y.index.to_series().dt.strftime('%Y-%m-%d').sort_values().unique()
    fig.add_trace(go.Scatter(x=y.index, y=y))   # x is date
    
    layout = dict(
        xaxis=dict(
            tickmode="array",
            tickvals=y.index[::2],   #  date
            ticktext=x_dates[::2],    # string
            tickformat='%Y-%m-%d',
            tickangle=45,
        )
    )
    
    fig.update_layout(layout)
    fig.show()
    
    

    still wrong result
    image

  8. nicolaskruchten commented on May 20, 2020

    @nicolaskruchten
    Contributor

    Ah, OK, the problem is that you're not seeing as many tick labels as you would expect, got it.

    There does seem to be a bug here, which I'll investigate, because while I get the expected output with:

    import plotly.graph_objects as go
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=["2010-01-01", "2010-02-01", "2010-03-01"], y=[1,3,2]))
    fig.update_xaxes(tickvals=["2010-01-15", "2010-02-15"], ticktext=["A","B"])
    fig.show()

    If I then manually set tickmode="array" I see the default ticks, even though setting tickvals internally is meant to set tickmode="array" !

  9. gvwilson commented on Jun 17, 2024

    @gvwilson
    Contributor

    Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions