# Plotly express scatter with date as xaxis

**URL:** <https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210>\
**Category:** 📊 Plotly Python\
**Created:** [April 6, 2023, 3:54pm UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210 "2023-04-06T15:54:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 6, 2023, 3:54pm UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/1 "2023-04-06T15:54:09Z")

</div>

Wen I try to render a scatter plot with the x-axis as date the values gets rounded when using express.

```python
fig = px.scatter(df, x="time", y="value", title="Durations", color='metric')

```

gives me a plot that looks like this

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/f/a/fa19d58cd131e8a58370c93f1cda4dfc54a2b3ad.png)

all values come in “columns”. When I instead use

```python
fig = go.Figure()
for metric, group in df.groupby("metric"):
    fig.add_trace(go.Scatter(x=group['time'], y=group['value'], mode='markers', name=metric))

```

I get the plot that I am aiming for, with correct x values

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/f/f/fff50ff0643b5306948bc5f7317da14cad74b544.png)

What am I doing wrong here or is it a bug?

Info of my data

```auto
# Column Non-Null Count Dtype                                
--- ------ -------------- -----                                
 0 metric 1550 non-null object                               
 1 time 1550 non-null datetime64[ns, pytz.FixedOffset(120)]
 2 value 1550 non-null float64                             

```

More relevant info is that hover data seems to be located at the correct x value so I get info while hovering empty space. If it is of any value I am using jupyter notebook and the same issue occurs both through vscode and jupyter-lab.

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [April 7, 2023, 2:45am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/2 "2023-04-07T02:45:50Z")

</div>

Can you share your sample df? I think maybe `fig.update_xaxes(type='category')` may help.

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 5:38am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/3 "2023-04-07T05:38:50Z")

</div>

sorry. the xaxes is datetime and if i do a `fig.update_xaxes(type = "category")` it works but it is not a datetime axis so it looks like this

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/a/6/a6d8b4751102120455d9fda01c6daad5326dde08.png)

Here is a sample from my data

| Index | Metric | Timee | Value |
| --- | --- | --- | --- |
| 19830 | metrica | 2023-04-06 15:45:43.488911+02:00 | 3.234 |
| 19572 | metricb | 2023-04-06 15:45:36.926151+02:00 | 0.346 |
| 4444 | metricb | 2023-04-06 15:38:32.102785+02:00 | 0.281 |
| 10500 | metricc | 2023-04-06 15:41:22.098974+02:00 | 1.432 |
| 45774 | metricb | 2023-04-06 15:58:27.443395+02:00 | 0.308 |
| 27192 | metrica | 2023-04-06 15:49:30.714646+02:00 | 2.101 |
| 9487 | metrica | 2023-04-06 15:40:54.835025+02:00 | 2.765 |
| 15035 | metricb | 2023-04-06 15:43:28.260221+02:00 | 0.643 |
| 44649 | metricd | 2023-04-06 15:58:00.867717+02:00 | 0.835 |
| 15461 | metricd | 2023-04-06 15:43:41.859967+02:00 | 3.702 |

with columns datatypes described above

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [April 7, 2023, 6:12am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/4 "2023-04-07T06:12:19Z")

</div>

Can you try `fig.update_xaxes(type='-')`. With your sample data I’m not sure it works as your expectation or not.

 ![newplot (20)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/c/e/ce3ac75fd3126359b1dba269c49b9aee6f6fe36c.png)

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 6:21am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/5 "2023-04-07T06:21:19Z")

</div>

I tried to get better test data for you and found something intereting

```python
dfsmall = df.head(1000)
fig = px.scatter(dfsmall, x="time", y="value", title="Durations", color='metric')

```

gave the correct output

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/e/c/eca432766ef38dc6d5881494d41d8ee176ed3200.png)

i tried 2000 and it looks wrong. Stepped down and found the breakpoint.

```python
dfsmall = df.head(1001)
fig = px.scatter(dfsmall, x="time", y="value", title="Durations", color='metric')

```

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/a/7/a7a58ddad251c41ff1d4428405fa1e75b704121e.png)

so above 1000 values seems wrong. Updating the xaxis does not impact this in any way

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [April 7, 2023, 6:23am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/6 "2023-04-07T06:23:54Z")

</div>

Might upload sample data to somewhere to download, I want to try to run it in my PC.

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 6:30am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/7 "2023-04-07T06:30:01Z")

</div>

i can recreate it doing:

```python
df = pd.DataFrame(columns=['metric', 'value', 'time'])
for i in range(1000):
    df = pd.concat([df, pd.DataFrame({'metric': random.choice(['a', 'b', 'c', 'd', 'e']), 'value': random.random()*20, 'time': datetime.datetime.now() + datetime.timedelta(seconds=random.randint(1, 300))}, index=[0])], ignore_index=True)

px.scatter(df, x="time", y="value", title="Durations", color='metric')

```

1000 values

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/f/2/f26e901eb863dfe0f937c0bda70915119f463e56.png)

1001 values

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/0/9/09a28d331f5342283cc787931e6bf3c512e794ae.png)

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [April 7, 2023, 6:36am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/8 "2023-04-07T06:36:52Z")

</div>

Quite strange, it worked for me with data over 1000. Which version of plotly are you using?

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/6/e/6e4ce7275f59421977f7e9e481c46049bd4db16c.png)

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 6:38am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/9 "2023-04-07T06:38:00Z")

</div>

```python
import plotly
plotly. __version__'5.12.0'

```

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [April 7, 2023, 6:39am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/10 "2023-04-07T06:39:32Z")

</div>

Can you try to update newest version of it. I’m using 5.13 and it worked.

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 6:43am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/11 "2023-04-07T06:43:57Z")

</div>

tried 5.14.1  
and pandas 2.0.0

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/5/c/5cc0bde4b638eb849e672aa82d0b74dea1231b19.png)

must be something wrong with my setup i guess.

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 7:02am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/12 "2023-04-07T07:02:28Z")

</div>

I created an issue on [github](https://github.com/plotly/plotly.py/issues/4154). Thanks for the help, I appreciate it!

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [April 7, 2023, 7:05am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/13 "2023-04-07T07:05:28Z")

</div>

Hope that you will find a solution, I am using notebook with windows laptop and it works fine.

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 7:11am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/14 "2023-04-07T07:11:06Z")

</div>

I will try the same thing in windows!

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [April 7, 2023, 9:56am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/15 "2023-04-07T09:56:46Z")

</div>

Hi there, I answered a topic with what it seems a similar issue with plotly.express. Somehow plotly.express groups the data.

> [@Stacked Bar chart is ordering data based on label category](https://community.plotly.com/t/stacked-bar-chart-is-ordering-data-based-on-label-category/73417):
>
> I am working on creating a visualization where each data point in the barchart is a duration in seconds (x-axis )and the color represents an event type such as as create, update, delete etc. I need all the events to be in a single bar and the the events should occur in the chart in the order they occur in the data with no grouping or sorting. Currently this seems to be grouping the events by type. I’m not sure if there is a better way to do this or not. I am fairly new to plotly’s library. I’ve…

In addition to that, I recall a topic where plotly.express switches the way of plotting the data internally when passing certain limit of data points but I can’t find it right now.

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 10:15am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/16 "2023-04-07T10:15:38Z")

</div>

the same code worked in windows btw

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [April 7, 2023, 10:17am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/17 "2023-04-07T10:17:20Z")

</div>

I can’t reproduce this neither on linux,

plotly: 5.13.1  
pandas: 1.5.1

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 10:27am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/18 "2023-04-07T10:27:30Z")

</div>

just tried linux myself and it works there as well. also reinstalled the library on mac and changed my kernel to the newest python version but still the same issue

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/8/0/809a5bfdc076b89a1cfbe3e25eeefa1993fd4af9.png)

it is not a really pressing issue since i can do it wot go figures but it is slightly annoying since it is more code to add margins etc. Also doing a line plot gives the correct results so it is only tied to scatter. Or I could change computer =)

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [April 7, 2023, 10:29am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/19 "2023-04-07T10:29:51Z")

</div>

I just tried upgrading to the newest versions of plotly and pandas (5.14.1, 2.0.0) on linux and it works.

So seems to be an issue on mac. Did you add the information to your github issue?

---

<div class="post-metadata">

**Author:** ![Olov](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/olov/32/23567_2.png) [@Olov](https://community.plotly.com/u/Olov)\
**Post date:** [April 7, 2023, 10:30am UTC](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210/20 "2023-04-07T10:30:25Z")

</div>

sure did. Added a comment of it working on windows. Should probably add that it only seems to be scatter since line works. I should perhaps dig up a intel mac and see if it has to do with Arm

Helpful community btw. I am not a Python developer and just tried to make some quick and dirty reports and try out pandas and plotly and I am pleasantly surprised.

[Next page](https://community.plotly.com/t/plotly-express-scatter-with-date-as-xaxis/74210.md?page=2)
