Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

The following are the steps to plot a stacked plot:

  1. Define the data for the plot:
x = np.array([1, 2, 3, 4, 5, 6], dtype=np.int32)
Apr = [5, 7, 6, 8, 7, 9]
May = [0, 4, 3, 7, 8, 9]
June = [6, 7, 4, 5, 6, 8]
  1. Define the list of labels to be used for the legend:
labels = ["April ", "May", "June"]
  1. Define the figure and the axes:
fig, ax = plt.subplots()
  1. Plot the stackplot and the legend:
ax.stackplot(x, Apr, May, June, labels=labels)
ax.legend(loc=2)
  1. Set the labels and the title:
plt.xlabel('defect reason code')
plt.ylabel('number of defects')
plt.title('Product Defects - Q1 FY2019')
  1. Display the figure on the screen:
plt.show()