Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python/Matplotlib dates are too close to each one

I have simple graph showing bitcoin price in some time as you can see on image.
enter image description here

Problem is that on x axis there are dates too close to each other. Thing is that I dont need to show every one date. Can I somehow limit them to lets say 6 labels instead of 25 as you can see on image right now. Or is there any better way how to handle dates?

My code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def plot_data_out(x: array, y: array):
    x = np.array(x,dtype=datetime)
    y = np.array(y,dtype=float)
    print(x)
    print(y)
    plt.plot(x, y)
    plt.show()

x data:

['2022-03-04 01:00:00' '2022-03-04 02:00:00' '2022-03-04 03:00:00'
 '2022-03-04 04:00:00' '2022-03-04 05:00:00' '2022-03-04 06:00:00'
 '2022-03-04 07:00:00' '2022-03-04 08:00:00' '2022-03-04 09:00:00'
 '2022-03-04 10:00:00' '2022-03-04 11:00:00' '2022-03-04 12:00:00'
 '2022-03-04 13:00:00' '2022-03-04 14:00:00' '2022-03-04 15:00:00'
 '2022-03-04 16:00:00' '2022-03-04 17:00:00' '2022-03-04 18:00:00'
 '2022-03-04 19:00:00' '2022-03-04 20:00:00' '2022-03-04 21:00:00'
 '2022-03-04 22:00:00' '2022-03-04 23:00:00' '2022-03-05 00:00:00'
 '2022-03-05 01:00:00']

y data

[42454.   41603.36 41510.58 41403.7  41449.93 41379.99 41374.45 41407.98
 41436.98 41590.   41522.29 41676.56 41662.06 41511.3  41315.75 41106.91
 40694.32 40754.41 40840.53 40605.88 39782.13 39469.36 39392.83 39004.73
 39148.65]

>Solution :

You can convert the x axis data to datetime format and then use it to plot the graph. Even then you can see the labels look congested. You can rotate the labels by 30 degrees and have more legible chart

v = ['2022-03-04 01:00:00','2022-03-04 02:00:00','2022-03-04 03:00:00',
 '2022-03-04 04:00:00','2022-03-04 05:00:00','2022-03-04 06:00:00',
 '2022-03-04 07:00:00','2022-03-04 08:00:00','2022-03-04 09:00:00',
 '2022-03-04 10:00:00','2022-03-04 11:00:00','2022-03-04 12:00:00',
 '2022-03-04 13:00:00','2022-03-04 14:00:00','2022-03-04 15:00:00',
 '2022-03-04 16:00:00','2022-03-04 17:00:00','2022-03-04 18:00:00',
 '2022-03-04 19:00:00','2022-03-04 20:00:00','2022-03-04 21:00:00',
 '2022-03-04 22:00:00','2022-03-04 23:00:00','2022-03-05 00:00:00',
 '2022-03-05 01:00:00']

y = [42454.,41603.36,41510.58,41403.7 ,41449.93,41379.99,41374.45,41407.98,
 41436.98, 41590. ,  41522.29, 41676.56 ,41662.06, 41511.3  ,41315.75, 41106.91,
 40694.32, 40754.41 ,40840.53, 40605.88, 39782.13 ,39469.36 ,39392.83 ,39004.73,
 39148.65]

v1 = [datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") for x in v]

import matplotlib.pyplot as plt
plt.plot(v1,y)
plt.xticks(rotation = 30)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading