Issue
I'm new to matplotlib, and for the last 3 days I've been trying to make a very simple horizontal bar graph:
In Y
it carries a products
list, in X
days_prod
, which is the number of days left for a specific product.
What I can't do is to add at the end of each bar the date the product is finished, which is obtained in eo_inv
.
I know there are several posts on this topic in this forum, but I can't figure out how to adapt them to my needs.
Here is the code:
from datetime import date, timedelta
import matplotlib.pyplot as plt
days_prod = [292, 268, 273, 249, 191, 251]
prods = ['milk', 'butter', 'curd', 'panner', 'cheese', 'yogurt']
eo_inv = [
str(date.today() + timedelta(days=x-1))
for x in days_prod
]
# at 2023/10/17 eo_inv results in:
# eo_inv = ['2024-08-03', '2024-07-10', '2024-07-15', '2024-06-21', '2024-04-24', '2024-06-23']
plt.barh(prods, days_prod)
plt.title('Inventory')
plt.xlabel('Days Left')
plt.ylabel('Products')
plt.show()
Solution
Maybe you are looking for something like iterating over all your entries in days_prod
and adding the corresponding entry in eo_inv
at the end of the bar:
plt.xlim(0, max(days_prod)*1.3)
for i, day in enumerate(days_prod):
plt.text(day, i, eo_inv[i])
Answered By - mcjeb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.