0

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:

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]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
JGeorgeJ
  • 361
  • 7
  • 20
  • You can also use the maplotlib date format. See this for [details](https://matplotlib.org/stable/gallery/ticks/date_concise_formatter.html). ` – r-beginners Mar 06 '22 at 13:35

1 Answers1

1

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)
Yagami_Light
  • 192
  • 1
  • 9
  • Only one thing if you know now in plot data are shown without minutes, seconds and year only "month-day hour" while x( in your case v1) data are correct daytime format with year and minutes – JGeorgeJ Mar 06 '22 at 12:53
  • 1
    The data doesnt have minutes and seconds i mean its zero in minutes and seconds for all the values that might be the reason why it is shown that way... We can manually give ticks and labels to the plot. That might help. – Yagami_Light Mar 06 '22 at 14:02