Hiddenguy
Hiddenguy

Reputation: 537

Incorrect xtick setting python

I've got an csv file, which contains five days of data, everything is set in few columns. The problem was that every measurement was set every 5 minutes, so during one day I have 288 rows, which for 5 days is 1440 and it goes like this (0:00, 0:05, 0:10 ...).

I used this code to plot everything in one plot, but somehow aranging xticks doesn't work properly.

Here is the code:

fig, ax = plt.subplots(1,1)
ax.set_xticks(x)
ax.set_xticklabels([v for v in data.Time], rotation=45)
ax.plot(x, data.Decfreq)
plt.xticks(np.arange(1, 1440, 60))

Plot I receive:

enter image description here

My data:

00:00 7.680827152169027 0.14000897718551028 7.600809170600135 0.23361947896117427 00:05 7.650820409080692 0.1564676061198724 7.530793436727354 0.2561764164383169 00:10 7.630815913688469 0.15549587808153068 7.540795684423466 0.2576230038042995 00:15 7.820858619914587 0.17966340911411277 7.540795684423466 0.28225658521669184 00:20 7.540795684423466 0.17165693216100902 7.50078669363902 0.2630767707044145 00:25 7.670824904472915 0.13538117325249963 7.390761968981794 0.24547505458369223 00:30 7.84086311530681 0.18094062831351296 7.630815913688469 0.26532083891716435 00:35 7.9608900876601485 0.14987576886445067 7.660822656776803 0.25499025558872285 00:40 7.200719262755675 0.12533028213451503 7.120701281186783 0.23856516035634334

Where only first (time) and second (data) columns interest me.

Implementing code by @Anwarvic I've got this:

enter image description here

Upvotes: 1

Views: 102

Answers (1)

Anwarvic
Anwarvic

Reputation: 12992

As I understood from your comment, the problem is within the label, not the ticks themselves. You need to skip 60 from the data.Time and change set_xticklabels just like so:

fig, ax = plt.subplots(1,1)
ax.set_xticks(x)
values = data.Time.values
ax.set_xticklabels([values[i] for i in range(0, len(values), 60)], rotation=45)
ax.plot(data.Decfreq)

And there is no need for the plt.xticks() part as it's the same as the ax.set_xticks().

EDIT

I don't know how come your plot is far different than mine. Here is my code using this sample data that I created to look exactly like yours:

data = pd.read_csv('sample.csv')
x = np.arange(0, 1440, 60)
fig, ax = plt.subplots(1,1)
ax.set_xticks(x)
# ax.set_xticklabels([v for v in data.Time], rotation=45)
values = data.Time.values
ax.set_xticklabels([values[i] for i in range(0, len(values), 60)], rotation=45)

ax.plot(data.Decfreq)
plt.show()

And here is the plot: enter image description here

So, my advice is to try changing my csv file with yours with few differences and see if it works

Upvotes: 1

Related Questions