Use plot to explore the results of using fmt strings in different situations. And explore the feasibility of replacing fmt strings with **kwargs in the plot0 function!

1. Import the pyplot template in the numpy library and matplotlib library, and set it to support Chinese character display:
#Import numpy library
#Import the pyplot module in the matplotlib library
import numpy as np
import matplotlib.pyplot as plt
 
# 0. Set up support for Chinese character display (fixed code, no need to change):
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

2. Create a class named Chart, which contains the usage method of fmt string, and the method of replacing fmt string with ** kwargs in the plot0 function:

# 1. Create a class named Chart:
class Chart:
    def __init__(self, y1, y2, y3, y4):
        self.y1 = y1
        self.y2 = y2
        self.y3 = y3
        self.y4 = y4
 
   # Usage method containing fmt string:
    def chart_bar_errorbar1(self):
        plt.bar(x, y1, bar_width)
        plt.bar(x + bar_width, y2, bar_width, tick_label=["Spring", "Summer", "Autumn"])
        plt.bar(x + 2 * bar_width, y3, bar_width)
        plt.bar(x + 3 * bar_width, y4, bar_width)
        plt.errorbar(x, y1, yerr=error1, capsize=3, elinewidth=2, fmt=',k')
        plt.errorbar(x + bar_width, y2, yerr=error2, capsize=3, elinewidth=2, fmt=',k')
        plt.errorbar(x + 2 * bar_width, y3, yerr=error3, capsize=3, elinewidth=2, fmt=',k')
        plt.errorbar(x + 3 * bar_width, y4, yerr=error4, capsize=3, elinewidth=2, fmt=',k')
        plt.show()
 
    def chart_bar_errorbar2(self):
        plt.bar(x, y1, bar_width)
        plt.bar(x + bar_width, y2, bar_width, tick_label=["Spring", "Summer", "Autumn"])
        plt.bar(x + 2 * bar_width, y3, bar_width)
        plt.bar(x + 3 * bar_width, y4, bar_width)
        plt.errorbar(x, y1, yerr=error1, capsize=3, elinewidth=2, fmt=',')
        plt.errorbar(x + bar_width, y2, yerr=error2, capsize=3, elinewidth=2, fmt=',')
        plt.errorbar(x + 2 * bar_width, y3, yerr=error3, capsize=3, elinewidth=2, fmt=',')
        plt.errorbar(x + 3 * bar_width, y4, yerr=error4, capsize=3, elinewidth=2, fmt=',')
        plt.show()
 
    def chart_bar_errorbar3(self):
        plt.bar(x, y1, bar_width)
        plt.bar(x + bar_width, y2, bar_width, tick_label=["Spring", "Summer", "Autumn"])
        plt.bar(x + 2 * bar_width, y3, bar_width)
        plt.bar(x + 3 * bar_width, y4, bar_width)
        plt.errorbar(x, y1, yerr=error1, capsize=3, elinewidth=2, fmt='k')
        plt.errorbar(x + bar_width, y2, yerr=error2, capsize=3, elinewidth=2, fmt='k')
        plt.errorbar(x + 2 * bar_width, y3, yerr=error3, capsize=3, elinewidth=2, fmt='k')
        plt.errorbar(x + 3 * bar_width, y4, yerr=error4, capsize=3, elinewidth=2, fmt='k')
        plt.show()
 
    # Contains the method of using ** kwargs in the plot0 function to replace the fmt string, where the data point type is marker, the data point color is color, and the line style of the data point connection line is linestyle
    def chart_bar_errorbar4(self):
        plt.bar(x, y1, bar_width)
        plt.bar(x + bar_width, y2, bar_width, tick_label=["Spring", "Summer", "Autumn"])
        plt.bar(x + 2 * bar_width, y3, bar_width)
        plt.bar(x + 3 * bar_width, y4, bar_width)
        plt.errorbar(x, y1, yerr=error1, capsize=3, elinewidth=2, marker='',color='k',linestyle='--')
        plt.errorbar(x + bar_width, y2, yerr=error2, capsize=3, elinewidth=2, marker='',color='k',linestyle='--')
        plt.errorbar(x + 2 * bar_width, y3, yerr=error3, capsize=3, elinewidth=2, marker='',color='k',linestyle='--')
        plt.errorbar(x + 3 * bar_width, y4, yerr=error4, capsize=3, elinewidth=2, marker='',color='k',linestyle='--')
        plt.show()
 
    def chart_bar_errorbar5(self):
        plt.bar(x, y1, bar_width)
        plt.bar(x + bar_width, y2, bar_width, tick_label=["Spring", "Summer", "Autumn"])
        plt.bar(x + 2 * bar_width, y3, bar_width)
        plt.bar(x + 3 * bar_width, y4, bar_width)
        plt.errorbar(x, y1, yerr=error1, capsize=3, elinewidth=2, marker=',', color='r', linestyle='--')
        plt.errorbar(x + bar_width, y2, yerr=error2, capsize=3, elinewidth=2, marker=',', color='r', linestyle='--')
        plt.errorbar(x + 2 * bar_width, y3, yerr=error3, capsize=3, elinewidth=2, marker=',', color='r', linestyle='--')
        plt.errorbar(x + 3 * bar_width, y4, yerr=error4, capsize=3, elinewidth=2, marker=',', color='r', linestyle='--')
        plt.show()
 
    def chart_bar_errorbar6(self):
        plt.bar(x, y1, bar_width)
        plt.bar(x + bar_width, y2, bar_width, tick_label=["Spring", "Summer", "Autumn"])
        plt.bar(x + 2 * bar_width, y3, bar_width)
        plt.bar(x + 3 * bar_width, y4, bar_width)
        plt.errorbar(x, y1, yerr=error1, capsize=3, elinewidth=2, marker=',',color='k',linestyle='')
        plt.errorbar(x + bar_width, y2, yerr=error2, capsize=3, elinewidth=2, marker=',',color='k',linestyle='')
        plt.errorbar(x + 2 * bar_width, y3, yerr=error3, capsize=3, elinewidth=2, marker=',',color='k',linestyle='')
        plt.errorbar(x + 3 * bar_width, y4, yerr=error4, capsize=3, elinewidth=2, marker=',',color='k',linestyle='')
        plt.show()

3. Prepare x-axis and y-axis data, and specify measurement deviation:

if __name__ == "__main__":
     # 2. Prepare data for x-axis and y-axis:
     x = np.arange(3)
     y1 = np.array([2.04, 1.57, 1.63])
     y2 = np.array([1.69, 1.61, 1.64])
     y3 = np.array([4.65, 4.99, 4.94])
     y4 = np.array([3.39, 2.33, 4.10])
 
     # 3. Specify measurement deviation:
     error1 = [0.16, 0.08, 0.10]
     error2 = [0.27, 0.14, 0.14]
     error3 = [0.34, 0.32, 0.29]
     error4 = [0.23, 0.23, 0.39]
     bar_width = 0.2

4. Create Chart object:

 # 4. Create Chart object:
     charts = Chart(y1, y2, y3, y4)

5. The results of using fmt string in different situations:

# 5. The results of using fmt string in different situations:
     # If the label type of the data point is specified in the fmt character but the line type (specified color) is not specified, the connecting line will not be created.
     charts.chart_bar_errorbar1()
 
     # If the fmt character specifies the label type of the data point but does not specify the line type (no color specified), the connecting line will not be created.
     charts.chart_bar_errorbar2()
 
     # If the color is specified in the fmt string but the line type is not specified (the label type is not specified), the connecting line will be created
     charts.chart_bar_errorbar3()

operation result:

6. Use **kwargs in the plot0 function to replace the fmt string, where the data point type is marker, the data point color is color, and the data point connecting line type is linestyle:

 # 6. Use **kwargs in the plot0 function to replace the fmt string, where the data point type is marker, the data point color is color, and the line style of the data point connecting line is linestyle:
     # Set marker='', color='k', linestyle='--'
     charts.chart_bar_errorbar4()
 
     # Set marker=',', color='r', linestyle='--'
     charts.chart_bar_errorbar5()
 
     # Set marker=',', color='k', linestyle=''
     charts.chart_bar_errorbar6()

operation result: