Examples of integration of Python function drawing and advanced algebra (2): Flashpoint function
1: Draw box plot
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np ''' Function: draw box plot Call signature: plt.boxplot(x) Parameter Description: x: input data for drawing box plot ''' #Set Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] #Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False x = np.random.randn(1000) # Box plot plt.boxplot(x) plt.xticks([1], ["Random number generator AlphaRM"]) plt.ylabel("Random value") plt.title("Stability of anti-interference ability of random number generator") plt.grid(axis="y", ls=":", lw=1, color="gray", alpha=0.4) plt.show()
2: Draw the box plot operation effect
Three: Draw an error bar graph
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np ''' Function: Draw the error range in the X|Y axis direction Call signature: plt.errorbar(x, y, fmt="bo:", yerr=0.2, xerr=0.02) Parameter Description: x: horizontal position of data point y: vertical position of data point yerr: error calculation method for data points in the y-axis direction xerr: error calculation method for data points in the x-axis direction ''' #Set Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] #Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False x = np.linspace(0.1, 0.6, 6) y = np.exp(x) plt.errorbar(x, y, fmt="bo:", yerr=0.2, xerr=0.02) plt.xlim(0, 0.7) plt.title("Error Bar Chart Example") plt.xlabel("Error range in X-axis direction") plt.ylabel("Error range in Y-axis direction") plt.show()
Four: Draw the error bar graph operation effect
Five: Draw a stacked column chart
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt ''' Stacked bar chart: Set the value of the parameter bottom of the function bar() to the list y, and the list y1 = [2, 6, 3, 8, 5, 9, 3, 9, 2, 9] represents another set of test papers Number of copies of test paper , the function bar(x,y1,bottom=y,color="r") will output a stacked histogram ''' #Set Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] #Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False # Some simple data x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y = [6, 10, 4, 5, 1, 14, 2, 6, 13, 3] y1 = [2, 6, 3, 8, 5, 9, 3, 9, 2, 9] #Create a histogram plt.bar(x, y, align="center", color="#66c2a5", tick_label=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"], label="Class A") #bottom=y means y is stacked below y1 plt.bar(x, y1, align="center", bottom=y, color="#8da0cb", label="班级B") #Set the label text of the x|y axis plt.xlabel("Test difficulty level") plt.ylabel("Number of Exam Papers") plt.legend() # label="Class B" display legend plt.title("Example of test paper difficulty and copy stacking chart") plt.show()