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

How it works...

Here is the explanation of the code:

  • x = np.array([1, 2, 3, 4, 5, 6], dtype=np.int32) is the list of product defect codes with the data type as the integer.
  • Apr = [5, 7, 6, 8, 7, 9] is the list of product defect counts by the defect code for the month of April.
  • May = [0, 4, 3, 7, 8, 9] is the list of product defect counts by the defect code for the month of May.
  • June = [6, 7, 4, 5, 6, 8] is the list of product defect counts by the defect code for the month of June.
  • labels = ["April ", "May", "June"] is the list of labels to be used as the plot legend.
  • fig, ax = plt.subplots() defines the figure and axes on which the graph is to be plotted.
  • ax.stackplot(x, Apr, May, June, labels=labels) plots the stacked plot with the given data and labels.
  • ax.legend(loc=2) specifies the legend to be plotted in the top left of the graph; we will learn more about legend locations in Chapter 4Developing Visualizations for Publishing Quality.
  • plt.xlabel('defect reason code') plots a label for the x axis.
  • plt.ylabel('number of defects') plots the y axis label.
  • plt.title('Product Defects - Q1 FY2019') plots the title for the graph.
  • plt.show() displays the figure on the screen.

On executing the preceding code, you should see the following figure on your screen: