1

So I do have a simple question. I have a program which simulates a week/month of living of a shop. For now it takes care of cashdesks (I don't know if I transalted that one correctly from my language), as they can fail sometimes, and some specialist has to come to the shop and repair them. At the end of simulation, program plots a graph which look like this:

enter image description here

The 1.0 state occurs when the cashdesk has gotten some error/broke, then it waits for a technician to repair it, and then it gets back to 0, working state.

I or rather my project guy would rather see something else than minutes on the x axis. How can I do it? I mean, I would like it to be like Day 1, then an interval, Day 2, etc.

I know about pyplot.xticks() method, but it assigns the labels to the ticks that are in the list in the first argument, so then I would have to make like 2000 labels, with minutes, and I want only 7, with days written on it.

minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57

2 Answers2

1

You can use matplotlib set_ticks and get_xticklabels() method of ax, inspired by this and this questions.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

minutes_in_day = 24 * 60

test = pd.Series(np.random.binomial(1, 0.002, 7 * minutes_in_day))

fig, ax = plt.subplots(1)
test.plot(ax = ax)

start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, minutes_in_day))

labels = ['Day\n %d'%(int(item.get_text())/minutes_in_day+ 1) for item in ax.get_xticklabels()]
ax.set_xticklabels(labels)

I get something like the picture below.

enter image description here

Community
  • 1
  • 1
FLab
  • 7,136
  • 5
  • 36
  • 69
1

You're on the right track with plt.xticks(). Try this:

import matplotlib.pyplot as plt

# Generate dummy data
x_minutes = range(1, 2001)
y = [i*2 for i in x_minutes]

# Convert minutes to days
x_days = [i/1440.0 for i in x_minutes]

# Plot the data over the newly created days list
plt.plot(x_days, y)

# Create labels using some string formatting
labels = ['Day %d' % (item) for item in range(int(min(x_days)), int(max(x_days)+1))]

# Set the tick strings
plt.xticks(range(len(labels)), labels)

# Show the plot
plt.show()
dpwilson
  • 997
  • 9
  • 19