Issue
I am trying to create 'Capacity chart' for production aims.
This chart is including information about "Plan value", "Current value" and "Late units". Yellow bar is a "Plan" and it is static for each bar. Green bar is a current value, showing how much pieces was produced for this time. And the last one is "Late units". If units were made too late - those become red color.
Just for example: on week 45 production needs to make 300 units. 62 (including 10 late units) units was produced. 10 units was produced on week 45, but should have been produced on week 44, that's why now it became red color.
My question is: how I can add red bar only to week 45? And not to each bar on whole plot. Now I have a result like this:
I can change code and get chart like this, but in this way I loosed week numbers.
import matplotlib.pyplot as plt
import numpy as np
plt.figure('Capacity chart 2023')
# create data
x = [42,43,44,45]
y1 = np.array(300) # plan
y2 = np.array([172, 132, 189, 62]) # fact
y3 = np.array(10) # late
# plot bars in stack manner
plt.bar(x, y1, color='yellow', align="center", tick_label=x) # plan
plt.bar(x, y2, color='green', align="center", tick_label=x) # fact
plt.bar(x, y3, color='red', align="center", tick_label=x) # late
ax = plt.gca()
for container in ax.containers:
plt.bar_label(container)
plt.title('Capacity chart 2023')
plt.ylabel('Units amount')
plt.xlabel('Week number')
plt.legend(["Plan value", "Current value", "Late units"], loc="upper right")
plt.get_current_fig_manager().window.state('zoomed')
plt.show()
If I add new list:
i = [45,45,45,45]
And change x position for red bar:
plt.bar(i, y3, color='red', align="center", tick_label=x) # late
I am getting red bar only on week 45, but like I sad above, I am loosed week numbers.
Is there is a possible way to show 3 stacked bar not at each bar, but only when it needed?
Solution
I am not sure if I fully understand. You can plot the "late units" bars on top of "current value" only where "late units" exists. Here's the example code:
import matplotlib.pyplot as plt
import numpy as np
plt.figure('Capacity chart 2023')
x = [42, 43, 44, 45]
y1 = np.array([300, 200, 250, 300])
y2 = np.array([172, 132, 189, 62])
late_units = np.array([0, 0, 0, 10])
# Plot the "plan value" bars
plt.bar(x, y1, color='yellow', align="center", label="Plan value")
# Plot the "current value" bars on top of "plan value"
plt.bar(x, y2, color='green', align="center", label="Current value", bottom=y1)
# Plot the "late units" bars on top of "current value"
for week, late_units_count in zip(x, late_units):
if late_units_count > 0:
plt.bar(week, late_units_count, color='red', align="center", label="Late units")
plt.title('Capacity chart 2023')
plt.ylabel('Units amount')
plt.xlabel('Week number')
plt.legend(loc="upper right")
plt.xticks(x)
plt.show()
Answered By - Guapi-zh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.