'xticks in matplotlib graph are not correctly spaced
I'm trying to make a graph using matplotlib but the spacing of the data on the x-axis is not correct. The data used on the Xaxis are 44 dates each with a subdivision of 24 hours, for example 2022-02-10-00h,2022-02-10-01h, ...
At first I got following graph with following code:
df2.plot.line()
plt.xticks(rotation=15)
plt.show()
But I would like that for each date the 00h is displayed (eg. 2022-02-10-00h)
I already tried to define the xticks:
amount_dates = len(xaxis_dates)
df2.plot.line()
plt.xticks(np.arange(0, amount_dates, step=1),xaxis_dates)
plt.xticks(rotation=15)
plt.show()
Which had the following array as output:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38]),
[Text(0, 0, '2022-02-15-h00'), Text(1, 0, '2022-02-16-h00'), Text(2, 0, '2022-02-17-h00'), Text(3, 0, '2022-02-18-h00'), Text(4, 0, '2022-02-19-h00'), Text(5, 0, '2022-02-20-h00'), Text(6, 0, '2022-02-21-h00'), Text(7, 0, '2022-02-22-h00'), Text(8, 0, '2022-02-23-h00'), Text(9, 0, '2022-02-24-h00'), Text(10, 0, '2022-02-25-h00'), Text(11, 0, '2022-02-26-h00'), Text(12, 0, '2022-02-27-h00'), Text(13, 0, '2022-02-28-h00'), Text(14, 0, '2022-03-01-h00'), Text(15, 0, '2022-03-02-h00'), Text(16, 0, '2022-03-03-h00'), Text(17, 0, '2022-03-04-h00'), Text(18, 0, '2022-03-05-h00'), Text(19, 0, '2022-03-06-h00'), Text(20, 0, '2022-03-07-h00'), Text(21, 0, '2022-03-08-h00'), Text(22, 0, '2022-03-09-h00'), Text(23, 0, '2022-03-10-h00'), Text(24, 0, '2022-03-11-h00'), Text(25, 0, '2022-03-12-h00'), Text(26, 0, '2022-03-13-h00'), Text(27, 0, '2022-03-14-h00'), Text(28, 0, '2022-03-15-h00'), Text(29, 0, '2022-03-16-h00'), Text(30, 0, '2022-03-17-h00'), Text(31, 0, '2022-03-18-h00'), Text(32, 0, '2022-03-19-h00'), Text(33, 0, '2022-03-20-h00'), Text(34, 0, '2022-03-21-h00'), Text(35, 0, '2022-03-22-h00'), Text(36, 0, '2022-03-23-h00'), Text(37, 0, '2022-03-24-h00'), Text(38, 0, '2022-03-25-h00')])
But now the x-axis spacing is not correct:
Thank you in advance for the help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


