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.

8
  • Your code works pretty well and gives the second picture on my system. Commented Jun 11, 2020 at 12:40
  • It gives the first one on my system. Incredibly frustrating. Commented Jun 11, 2020 at 12:42
  • My coworker also gets the first one in his system. Commented Jun 11, 2020 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 Commented Jun 11, 2020 at 12:44
  • Can you show how you would change the code? Commented Jun 11, 2020 at 12:46

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

Putting:

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

After the scatter line solved it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.