1

This is a first time I am working with matplotlib and the task might be really trivial, but turns out to be hard for me.

I have following data: tickets numbers, dates when they were resolved and dates when whey were supposed to be resolved.

What I want to do is to draw a plot with tickets on x axis and dates on y axis. Then for every ticket I need to have 2 bars: first one with height equal to the date it was resolved, and another one with height equal to the date is was supposed to be resolved.

What I have right now:

  • a list of all tickets

    tickets = []
    
  • a list with all dates (both resolved and expected)

    all_dates = []
    

lists with sets of (ticket, datetime) for expected time and resolved time:

tickets_estimation = []
tickets_real = []

the code I am at right now:

plt.xticks(arange(len(tickets)), tickets, rotation=90)
plt.yticks(arange(len(all_dates)), all_dates)
plt.show()

which shows me following plot: plot

So how can I do the rest? Please pay attention that I need to map tickets numbers at X axis to the dates on Y axis.

Ok, here is is simplified at where I stack: I cannot figure out how to draw even a single bar so its X axis will be a ticket and its Y axis will be a date of it's resolution. For example:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from numpy import arange

date = ['3 Jan 2013', '4 Jan 2013', '5 Jan 2013']
tickets = ['ENV-666', 'ENV-999', 'ENV-1000']


# Convert to matplotlib's internal date format.
y = mdates.datestr2num(date)
x = arange(len(tickets))

fig, ax = plt.subplots()

ax.plot(x,y)
ax.yaxis_date()

# Optional. Just rotates x-ticklabels in this case.
fig.autofmt_xdate()
plt.show()

This works find, it shows a plot with a line. But if I change ax.plit(x,y) to ax.bar(x,y) I am receiving an error: ValueError: ordinal must be >= 1

tmdavison
  • 64,360
  • 12
  • 187
  • 165
Roman
  • 624
  • 5
  • 15
  • No, not even close to what I need. – Roman Jan 31 '18 at 13:12
  • If this is not what you need, you need to make clear why that won't solve the issue. (Note that something like "The other question has kilograms on the axis, but I want meters" is of course no valid reason. Instead one would need to see the actual problem - why can't you show two bars just as in the linked question?) – ImportanceOfBeingErnest Jan 31 '18 at 13:21
  • I did: Please pay attention that I need to map tickets numbers at X axis to the dates on Y axis. – Roman Jan 31 '18 at 13:22
  • Sure. I did understand that. But what is the problem of doing this? (I'm not saying that there might not be a problem with it, it just isn't clear from the question. As the question currently reads, you are asking how to graph two groups of bars on a chart and that is indeed shown in the duplicate.) – ImportanceOfBeingErnest Jan 31 '18 at 13:24
  • Two groups of bars is a question number 2 I would say. Right now I cannot understand how can I plot a bar for a ticket X so it would be of the height Y – Roman Jan 31 '18 at 13:26
  • It is perfectly fine not to understand something, but it needs to be clear what the problem is. `plt.bar(x,y)` plots bars at positions x and height y. Why is it not possible to use this here? This is what the question needs to be specific about. – ImportanceOfBeingErnest Jan 31 '18 at 13:30
  • I have added a section with description of what exactly is not working. – Roman Jan 31 '18 at 13:41
  • 1
    Ok, that makes sense now. The problem is that bars usually start at 0. But for dates there is no 0. Hence you need to specify `bottom`. – ImportanceOfBeingErnest Jan 31 '18 at 14:03

1 Answers1

0

Bar is your friend: https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.bar.html

Then just use your fantasy to plot it as you like. For example a black line for the due date, red boxes for overdue and green boxes for on time.

supp = np.array([3,5,7])
res = np.array([1,7,7])
H = res-supp
H[H==0] = 0.01
ind = np.arange(len(D1))

plt.bar(ind[H>0], H[H>0], bottom=supp[H>0], color='r');
plt.bar(ind[H<0], H[H<0], bottom=supp[H<0], color='g');
plt.bar(ind, 0.1, bottom=supp, color='k');

enter image description here

scholi
  • 314
  • 2
  • 10
  • The representation is good, but what I don't understand is to how map the ticket to it's resolution date on Y axis. – Roman Jan 31 '18 at 13:18
  • I'm not sure of what you don't understand. In my example the variable res gives the resolution dates (here I used integers instead of dates, but the problem is the same), while supp set the "supposed date" (here again int in my example). – scholi Feb 01 '18 at 09:08