Examples of integration of Python function drawing and advanced algebra (2): Flashpoint function
1: X-axis bar graph drawing
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt ''' Use simple statistical functions to draw simple graphics function bar()----------used to draw columns Chart Function: Plot the respective characteristics of qualitative data on the x-axis Call signature: plt.bar(x, y) Parameter description: x: The type of qualitative data marked on the x-axis y: The number of each type of qualitative data ''' mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False # Use some simple data to draw x = [1, 2, 3, 4, 5, 6, 7, 8] y = [3, 1, 4, 5, 8, 9, 7, 2] # Create a histogram plt.bar(x, y, align="center", color=" c", tick_label=["q", "a", "c", "e", "r", "j", "b", "p"],hatch="/") # Set the X|Y axis label text name plt.xlabel("Box Number") plt.ylabel("Box Weight (kg)") plt.show()
2: Bar graph drawing operation effect
Three: Y-axis bar graph drawing
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt ''' Function: Plot the respective characteristics of qualitative data on the y-axis Call signature: plt.barh(x, y) parameters Description: y: The type of qualitative data marked on the x-axis x: The number of each type of qualitative data ''' # Set the Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] # Set the normal display symbols mpl.rcParams["axes.unicode_minus"] = False # Use some simple data to draw x = [1, 2, 3, 4, 5, 6, 7, 8] y = [3, 1 , 4, 5, 8, 9, 7, 2] # Create a bar chart plt.barh(x, y, align="center", color="c", tick_label=["q", "a", "c ", "e", "r", "j", "b", "p"],hatch="/") # Set the X|Y axis label text name plt.xlabel("Box weight (kg)") plt.ylabel("Box number") plt.show()
Four: Y-axis bar graph drawing operation effect
Five: Histogram drawing
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np ''' Function: Plot the distribution characteristics of quantitative data on the X axis Call signature: plt.hist(x ) Parameter description: x: Draw the quantitative data input axis of the box on the x-axis ''' # Set the Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] # Set the normal display symbol mpl .rcParams["axes.unicode_minus"] = False boxWeight = np.random.randint(0, 10, 50) x = boxWeight bins = range(0, 10, 1) plt.hist(x, bins=bins, color= "g", histtype="bar", rwidth=0.9, # This parameter controls the width of each column of the histogram alpha=0.3,label="Respective characteristics of quantitative data plotted on the X axis") # Set the X|Y axis label text namelabel="Plot separate characteristics of quantitative data on the X-axis") plt.xlabel("Box weight (kg)") plt.ylabel("Sales quantity (units)") ''' Function function: Add the title of the graphic content Call signature: plt.title("Sine function graphic") Parameter description: string: The title text of the graphic content ''' plt .title("Drawing histogram") plt.show()
Six: Histogram drawing operation effect
Seven: Drawing of pie chart
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt ''' Function function: Plot different types of percentages of qualitative data Call signature: plt.pie(x) Parameter description: x: Percentages of different types of qualitative data ''' # Set the Chinese display font mpl.rcParams["font.sans-serif"] = ["SimHei"] # Set the normal display symbol mpl.rcParams["axes.unicode_minus"] = False kinds = ['Simple box', 'Insulated box', 'Travel box', 'Sealed box'] colors = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3'] soldNums = [0.05 , 0.45, 0.15, 0.35] plt.pie(soldNums, labels=kinds, autopct="%3.1f%%", startangle=60, colors=colors) plt.title("Proportion of sales quantity of different types of boxes") plt.show()
Eight: Pie chart drawing operation effect
Nine: Drawing parallel column charts
# -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np ''' Side-by-side histogram drawing: ''' # Set the Chinese display font mpl.rcParams["font.sans -serif"] = ["SimHei"] # Set normal display symbols mpl.rcParams["axes.unicode_minus"] = False # Provide some simple data x = np.arange(5) # Two sets of list data Y1 = [100, 68, 79, 91, 82] y2 = [120, 75, 70, 78, 85] std_error1 = [7, 2, 6, 10, 5] std_error2 = [5, 1, 4, 8, 9] error_attr = dict(elinewidth=2, ecolor='black', capsize=3) bar_width = 0.4 tick_label = ['Park 1', 'Park 2', 'Park 3', 'Park 4','Park Five'] # Create a stacked bar chart plt.bar(x, y1, bar_width, color='#87CbEB', align='center', yerr=std_error1, error_kw=error_attr, label='2021') plt.bar(x + bar_width, y2, bar_width, color='#CD5C5C', align=' center', yerr=std_error2, error_kw=error_attr, label='2022') plt.xlabel("Mango planting area") plt.ylabel("Harvest volume") plt.xticks(x + bar_width / 2, tick_label) plt .title("Single harvest volume in mango planting areas in different years") plt.grid(True, axis='y', ls=":", color="yellow", alpha=0.8) plt.legend() plt.show()