Use a list of number string as xaxis

I’m trying to use a list of number string as xaxis.

I encountered two problems:

  1. The xaxis value is wrong;
  2. No dots is shown

Here’s the example:

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

x = ['0501', '0502', '0503', '0504', '0505', 
     '0506', '0507', '0508', '0509', '0510', 
     '0511', '0512', '0513', '0514', '0515', 
     '0516', '0517', '0518', '0519', '0520', 
     '0521', '0522', '0523', '0524', '0525', 
     '0526', '0527', '0528', '0529', '0530', 
     '0531']

nan = np.nan
y = np.array([           nan,          nan,          nan,          nan,          nan,
                         nan,          nan,          nan,  33.56146077,          nan,
                         nan,          nan,          nan,          nan,          nan,
                         nan,          nan,          nan,          nan,          nan,
                         nan,          nan,          nan,          nan,          nan,
                 36.5123643 ,          nan,          nan,          nan, 115.36623731,
                         nan])

trace = go.Scatter(
    x = x,
    y = y
)

data = [trace]

py.iplot(data)

Hi @zxdawn,

The reason you’re not seeing any markers seems to be that plotly.js is automatically setting the scatter.mode option to 'lines', and this data set doesn’t have any consecutive non-nan values so no lines can be drawn. If you set scatter.mode to 'markers' then you get this

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

x = ['0501', '0502', '0503', '0504', '0505', 
     '0506', '0507', '0508', '0509', '0510', 
     '0511', '0512', '0513', '0514', '0515', 
     '0516', '0517', '0518', '0519', '0520', 
     '0521', '0522', '0523', '0524', '0525', 
     '0526', '0527', '0528', '0529', '0530', 
     '0531']

nan = np.nan
y = np.array([           nan,          nan,          nan,          nan,          nan,
                         nan,          nan,          nan,  33.56146077,          nan,
                         nan,          nan,          nan,          nan,          nan,
                         nan,          nan,          nan,          nan,          nan,
                         nan,          nan,          nan,          nan,          nan,
                 36.5123643 ,          nan,          nan,          nan, 115.36623731,
                         nan])

trace = go.Scatter(
    x = x,
    y = y,
    mode = 'markers'
)

data = [trace]

py.iplot(data)

I’m not sure I follow what you mean about the xaxis values being wrong. Could you explain what you would like to see?

-Jon

Hi @jmmease,

  1. For xaxis values, I want it to be as same as strings. As you can see, it’s ‘500’, ‘505’ … It should be ‘0500’, ‘0505’;
  2. If the length of list is short, it works fine without scatter.mode: If neighbor value isn’t nan, they’ll be connected; If not, just plot the scatter.
import plotly
import plotly.graph_objs as go
import numpy as np

x = ['0501', '0502', '0503', '0504', '0505']

nan = np.nan
y = np.array([nan, 1, nan, 2, 3])

trace = go.Scatter(
    x = x,
    y = y
)

data = [trace]
py.iplot(data)

For xaxis values, I want it to be as same as strings. As you can see, it’s ‘500’, ‘505’ … It should be ‘0500’, ‘0505’;

As long as the x values are unique, you can do this by setting layout.xaxis.type = 'category'

...
data = [trace]
layout = go.Layout(xaxis={'type': 'category'})
fig = go.Figure(data, layout)

If the length of list is short, it works fine without scatter.mode : If neighbor value isn’t nan, they’ll be connected; If not, just plot the scatter.

If you always want to show markers, and you want to show lines when consecutive points are non-nan you can set scatter.mode to 'lines+markers'.

Hope that helps!
-Jon

1 Like