'How can I change the intervals of my Y axis which are currently MM:YY to have less intervals?

New to Python. I am working on a project where my x coordinates are numbers and my y coordinates are times (i.e 18:47).

So far I have written the following code

import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np


y= ['18:47','20:25','23:14','23:53','00:15','02:31','03:24','03:59','20:58','22:09','22:51', '23:10','23:24', '23:27','23:33','23:48','23:57','02:13','21:29','22:48','23:23','23:31','23:51','00:19','02:21', '22:08', '22:10', '22:15', '22:28', '22:43', '23:11','22:26','22:27','22:40', '22:42', '22:43', '22:46', '22:55', '23:04', '00:14', '00:36','23:11', '23:45']


x= [966.98, 1140.88,1139.97,1252.51,278.59,11.43,310.37,336.84,849.81,1034.66,873.24,8.45,898.03,780.19,821.96,20.94,880.31,317.59,826.89,1004.95,821.89,875.38,747.9,283.65,305.25,1147.88,919.1,666.71,734.29,999.15,1030.86,745.05,1318.89,984.28,1061.37,1008.38,926.72,1118.24,1183.93,333.42,298.76,881.12,781.06]

plt.xlabel('Transaction Amount')
plt.ylabel('Time of Transaction')


                  
plt.scatter(x,y)

However when the graph appears it looks like so:

I've tried using ticks and np.arange but it isn't working for me, does anyone have an idea how to make the intervals more spread out? As at the moment it looks like a complete mess!

enter image description here



Solution 1:[1]

First of all, you need to convert y from a list of str to a list of datetime:

y = pd.to_datetime(y, format = '%H:%M')

Then you can proceed to draw the plot, preferably using the Object Oriented Interface:

fig, ax = plt.subplots()

ax.scatter(x, y)

ax.set_xlabel('Transaction Amount')
ax.set_ylabel('Time of Transaction')

Finally, you can customize y axis ticks interval and format with:

ax.yaxis.set_major_locator(md.HourLocator(interval = 2))
ax.yaxis.set_major_formatter(md.DateFormatter('%H:%M'))

Complete code

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md


x = [966.98, 1140.88, 1139.97, 1252.51, 278.59, 11.43, 310.37, 336.84, 849.81, 1034.66, 873.24, 8.45, 898.03, 780.19,
     821.96, 20.94, 880.31, 317.59, 826.89, 1004.95, 821.89, 875.38, 747.9, 283.65, 305.25, 1147.88, 919.1, 666.71,
     734.29, 999.15, 1030.86, 745.05, 1318.89, 984.28, 1061.37, 1008.38, 926.72, 1118.24, 1183.93, 333.42, 298.76,
     881.12, 781.06]

y = ['18:47', '20:25', '23:14', '23:53', '00:15', '02:31', '03:24', '03:59', '20:58', '22:09', '22:51', '23:10',
     '23:24', '23:27', '23:33', '23:48', '23:57', '02:13', '21:29', '22:48', '23:23', '23:31', '23:51', '00:19',
     '02:21', '22:08', '22:10', '22:15', '22:28', '22:43', '23:11', '22:26', '22:27', '22:40', '22:42', '22:43',
     '22:46', '22:55', '23:04', '00:14', '00:36', '23:11', '23:45']

y = pd.to_datetime(y, format = '%H:%M')


fig, ax = plt.subplots()

ax.scatter(x, y)

ax.set_xlabel('Transaction Amount')
ax.set_ylabel('Time of Transaction')

ax.yaxis.set_major_locator(md.HourLocator(interval = 2))
ax.yaxis.set_major_formatter(md.DateFormatter('%H:%M'))

plt.show()

enter image description here

Solution 2:[2]

Here is the answer. You have to convert the variable y from list of strings to time format and then plot it in matplotlib.

import pandas as pd
import matplotlib.pyplot as plt
import datetime



y= ['18:47','20:25','23:14','23:53','00:15','02:31','03:24','03:59','20:58','22:09','22:51', '23:10','23:24', '23:27','23:33','23:48','23:57','02:13','21:29','22:48','23:23','23:31','23:51','00:19','02:21', '22:08', '22:10', '22:15', '22:28', '22:43', '23:11','22:26','22:27','22:40', '22:42', '22:43', '22:46', '22:55', '23:04', '00:14', '00:36','23:11', '23:45']
dates = pd.to_datetime(y, format='%H:%M')
lstDateTime = dates.to_list()

x= [966.98, 1140.88,1139.97,1252.51,278.59,11.43,310.37,336.84,849.81,1034.66,873.24,8.45,898.03,780.19,821.96,20.94,880.31,317.59,826.89,1004.95,821.89,875.38,747.9,283.65,305.25,1147.88,919.1,666.71,734.29,999.15,1030.86,745.05,1318.89,984.28,1061.37,1008.38,926.72,1118.24,1183.93,333.42,298.76,881.12,781.06]

plt.xlabel('Transaction Amount')
plt.ylabel('Time of Transaction')


plt.scatter(x,lstDateTime)

Plot looks like this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zephyr
Solution 2 habib