1

When running the following code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md
from itertools import product

Time = [f'2017-03-13 {H}:{M}:{S}' for H, M, S in list(product([('0' + str(x))[-2:] for x in range(0, 24)],
                                                              [('0' + str(x))[-2:] for x in range(0, 60)],
                                                              [('0' + str(x))[-2:] for x in range(0, 60)]))]
Speed = list(130*np.random.rand(len(Time)))
Kilometer = list(50*np.random.rand(len(Time)))
N130317 = pd.DataFrame({'Time':Time, 'Speed':Speed, 'Kilometer':Kilometer})

N130317['Time'] = pd.to_datetime(N130317['Time'], format = '%Y-%m-%d %H:%M:%S')

marker_size = 1  # sets size of dots
cm = plt.cm.get_cmap('plasma_r') #sets colour scheme
plt.scatter(N130317['Kilometer'], N130317['Time'], marker_size, c=N130317['Speed'], cmap=cm)
ax=plt.gca()
xfmt = md.DateFormatter('%H:%M')
ax.yaxis.set_major_formatter(xfmt)
plt.title("NDW 13-03-17")
plt.xlabel("Kilometer")
plt.ylabel("Time")
plt.colorbar().set_label("Speed", labelpad=+1) #Makes a legend
plt.show()
  • I get a graph that looks like this:

enter image description here

But I want it to look like this (made by another user):

enter image description here

Does anyone know the reason for this problem?

Matplotlib: Automatically displaying time column as 2 hour ticks on y axis in scatterplot - previous question that I got the solution from.

I same problem occurs before and after I have updated conda and all the packages.

nielsen
  • 383
  • 1
  • 6
  • Your code works pretty well and gives the second picture on my system. – Quang Hoang Jun 11 '20 at 12:40
  • It gives the first one on my system. Incredibly frustrating. – nielsen Jun 11 '20 at 12:42
  • My coworker also gets the first one in his system. – nielsen Jun 11 '20 at 12:44
  • I saw that you are using `plt.scatter` but formatting the datetime in hour minutes using `ax`. Try creating a `fig, ax = plt.subplots()`. Then use `ax.scatter` to plot the graph – XXavier Jun 11 '20 at 12:44
  • Can you show how you would change the code? – nielsen Jun 11 '20 at 12:46
  • I did not try but make these changes and see if it helps. `fig, ax = plt.subplots()` `marker_size = 1 # sets size of dots` `cm = plt.cm.get_cmap('plasma_r')` `ax.scatter(N130317['Kilometer'], N130317['Time'], marker_size, c=N130317['Speed'], cmap=cm)` `ax=plt.gca()` `xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')` `ax.yaxis.set_major_formatter(xfmt)` – XXavier Jun 11 '20 at 12:49
  • Didn't help but I found the solution – nielsen Jun 11 '20 at 12:58
  • See below the code that worked for me. – XXavier Jun 11 '20 at 12:58

2 Answers2

0

Please try this. Replace your code with the code below. It gave me the right heatmap. It is because of the way matplotlib format the datetime. Please see the discussion here

import matplotlib.dates as mdates    
fig, ax = plt.subplots()
marker_size = 1  # sets size of dots
cm = plt.cm.get_cmap('plasma_r') #sets colour scheme
ax.scatter(N130317['Kilometer'], mdates.datestr2num(N130317['Time'].astype('str')), marker_size, c=N130317['Speed'], cmap=cm)
    ax=plt.gca()

xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.yaxis.set_major_formatter(xfmt)
XXavier
  • 1,206
  • 1
  • 10
  • 14
0

Putting:

plt.ylim(N130317['Time'][0], N130317['Time'][N130317.index[-1]])

After the scatter line solved it.

nielsen
  • 383
  • 1
  • 6