How can I convert the caption from the x-axis from seconds to minutes?
With the data I have a generated label of 10xx, 20xx, 30xx and 40xx in seconds because the data has also an x-label in seconds.
Unfortunately, in order to be able to easy classify the values, it should be minutes and preferably at even times, such as every 30 or 60 minutes, depending on the amount of data.
The label of the x-axis should be something like this:
[60,120,180, 240,...]
df = pd.DataFrame(data)
df = df[['seconds', 'marker', 'data1', 'data2', 'data3']]
ax = df.set_index('seconds').plot(kind='bar', stacked=True, alpha=set_alpha)
ax.xaxis.set_major_locator(plt.MaxNLocator(5))
plt.plot(df.index, df['data1'], linestyle='solid', color='blue', alpha=0.4, label='data1')
plt.show()
Data example:
seconds,marker,data1,data2,data3,data4
0,B,0,0,0,0
59,C,42,8,369000,0
74,B,42,8,369000,283041
121,B,42,8,369000,283041
179,B,42,8,369000,283041
239,B,42,8,369000,283041
304,B,42,8,369000,283041
360,B,42,8,369000,283041
377,A,42,8,369000,283041
420,B,42,8,369000,283041
493,B,42,8,369000,283041
540,B,42,8,369000,283041
600,B,42,8,369000,283041
659,B,42,8,369000,283041
719,B,64,8,412000,283041
780,B,64,8,412000,283041
840,B,64,8,412000,283041
880,A,64,8,412000,283041
900,B,64,8,412000,283041
961,B,64,8,412000,283041
1020,B,64,8,412000,283041
1079,B,64,8,412000,283041
1141,B,64,8,412000,283041
1200,B,64,8,412000,283041
1260,B,64,8,412000,283041
1320,B,64,8,412000,283041
1365,A,64,8,412000,283041
1382,B,64,8,412000,283041
1440,B,64,8,412000,283041
1498,B,64,8,412000,283041
1559,B,64,8,412000,283041
1621,B,64,8,412000,283041
1679,B,64,8,412000,283041
1740,B,64,8,412000,283041
1800,B,42,8,369000,283041
1830,A,42,8,369000,283041
1867,B,42,8,369000,283041
1921,B,42,8,369000,283041
1979,B,42,8,369000,283041
2040,B,42,8,369000,283041
2099,B,42,8,369000,283041
2159,B,42,8,369000,283041
2220,B,42,8,369000,283041
2272,A,42,8,369000,283041
2288,B,42,8,369000,283041
2341,B,42,8,369000,283041
2400,B,42,8,369000,283041
2460,B,42,8,369000,283041
2520,B,42,8,369000,283041
2579,B,42,8,369000,283041
2640,B,42,8,369000,283041
2700,B,42,8,369000,283041
2720,A,42,8,369000,283041
2759,B,42,8,369000,283041
2833,B,28,14,248000,260096
2880,B,28,14,248000,247808
2940,B,14,28,124000,123904
3000,B,0,42,0,0
3060,B,0,42,0,0
3120,B,0,42,0,0
3136,A,0,42,0,0
3180,B,0,42,0,0
3251,B,0,42,0,0
3267,D,0,42,0,0
3300,B,0,42,0,0
3359,B,0,42,0,0
3419,B,0,42,0,0
3538,B,0,0,0,0
3599,B,0,0,0,0
3643,C,140,4,1260000,0
CodePudding user response:
I think you can play around with .set_xticks(ticks, labels=labels). Here is an example:
n = np.random.randint(3600, 10000) # obtain random maximal timestamp
t = np.linspace(0, n, 1000) # create 1000 time stamps
data = 5*t*np.exp(-t/100) # and some fake data
max_t = np.max(t) # seconds
max_t = max_t/60 # minutes
max_t = max_t/30 # half hours
max_t = np.floor(max_t) # round downwards
ticks = np.arange(0,max_t 1) # Now we now have many ticks you need
labels = ["%.1f h" % (x/2) for x in ticks] # Generate tick labels
xticks = ticks*30*60 # and the actual positions of the ticks
fig, ax = plt.subplots()
ax.plot(t, data)
ax.set_xticks(xticks, labels=labels)
plt.show()