2 x-axes labels of type Timestamp Python

I am looking to develop a graph with two x-axes labels of type pandas._libs.tslibs.timestamps.Timestamp. The first will have monthly labels and the second will have quarterly labels (in different colors). However, I cannot get two different axes labels to show up with type Timestamp. Below is a working example for type int.

WORKING - Plotly Graph with 2 x-axis labels of type int

import plotly.graph_objects as go

fig = go.Figure(data=None, layout=None)

fig.add_trace(
  go.Scatter(
    x=[1,2,3],
        y=[4,5,6],
        xaxis='x1',
        mode="lines",
  )
)

fig.add_trace(
  go.Scatter(
    x=[1,2,3],
        y=[4,5,6],
        xaxis='x2',
        mode="lines",
  )
)

layout=dict(
      yaxis=dict(title='yaxis title'), 
        xaxis=dict(
            overlaying= 'x',
            tickmode= 'linear',
            tick0= 1.1,
            dtick= 1,
            side= 'bottom',
            color='blue'
        ),
        xaxis2=dict(
            overlaying= 'x',
            tickmode= 'linear',
            tick0= 1,
            dtick= 1,
            side= 'bottom',
            color='red'
        ),
)

fig.layout=layout
fig.show()

NOT WORKING - Plotly Graph with 2 x-axis labels of type pandas._libs.tslibs.timestamps.Timestamp

Note: this is an incomplete code sample. If I can provide more information on the inputs, please let me know.

import plotly.graph_objects as go

fig = go.Figure(data=None, layout=None)
#x is a pandas series of type "pandas._libs.tslibs.timestamps.Timestamp"
#y is a pandas series of type "numpy.float64"

#add price plot
fig.add_trace(
    go.Scatter(
        x=x,
        y=y,
        xaxis='x1',
        mode="lines"
    ),
    row=1, col=1 #there are subplots in the graph
)

#add price plot
fig.add_trace(
    go.Scatter(
        x=x,
        y=y,
        xaxis='x2',
        mode="lines"
    ),
    row=1, col=1 #there are subplots in the graph
)
layout=dict(
      yaxis=dict(title='yaxis title'), 
        xaxis = dict(
            overlaying='x',
            tickmode='linear',
            tick0=days[30],
            dtick="M3",
            side='bottom',
            color='red'
        ),
        xaxis2 = dict(
            overlaying='x',
            tickmode='linear',
            tick0=days[30],
            dtick="M1",
            side='bottom',
            color='blue'
        )
)

fig.layout=layout
fig.show()

Here are the two most useful links so far:

Any help is greatly appreciated! I have been stuck on this for a week.

Hey,

I’m a little unsure of what exactly you are asking but I think I can help. Are the 2 traces the same y values? if so, I would just add one trace, and define x as 2 separate lists one that contains the timestamp (or months if you just want months) and the other that has a corresponding Quarter label.

I attached an image of what I think you want, let me know if you want something different.

-Lu

Hi Lu - thanks for the response!

Yes the y-values are the same. I am looking to create an x-axis that is show in the image below (there are different intervals):


Doing everything in once trace does seem to be the most efficient, but I can’t even get a second x-axis label to show up with datatype Timestamp. Could you provide the code for that graphic?

Moreover, because of the different intervals in the x-axis tick marks, I think it might be necessary to have two traces. What are your thoughts?

Thank you so much for the help! Your answer is essentially what I’m looking to do.

Michael

So I attached something that is not exactly like you want it, but should do the trick:

Basically what I did was make x = [q_labels,date_labels] with both of these being a list. I converted the timestamp to datetime.date because I didn’t want to see the time in the labels. This is only on 1 trace.

If you want exactly as you pasted, I would suggest looking at this:

and use multiple x-axis on 2 traces.

-L

Thanks so much for the help your solution pointed me in the right direction!

1 Like

Hi Louis

this is exactly what I am looking for. Do you mind sharing the code as I am a fairly new to Ploty.

Thank you !
S.

Hi, can you send me the example code to do this 2 x-axes labels ?
s693236305@gmail.com

I’ve been stucked this for 2 weeks.

Hi. This is what I want but would you please share the example of your code here please?

Hope to hear from you soon!

Thank you in advance!

I have tried something new by introducing color of the bars as shown here. May be this could be useful to u.

Here is the example of how my plot looks like