0. Common statistical charts for Matplotlib
Common statistical charts in Matplotlib include line charts, scatter plots, histograms, bar charts, etc. Among them, a line chart is generally used to describe the changes in data. Through the line chart, the trend of data changes can be clearly seen, such as the time line trend in the stock market.
Scatter plots are used to find the relationship between variables (x, y), display outliers, analyze potential connections, etc., such as the pig cycle, the correspondence between 24 solar terms and variable inventory, and the relationship between gold prices and the US dollar exchange rate.
Histograms are used to display the distribution of quantitative data, such as grayscale histograms, color histograms, gradient histograms, etc. in image analysis.
The bar chart, on the other hand, groups the classified objects instead of grouping them based on specific numerical values, and the grouping order can be adjusted. For example, the GDP situation of different countries, and the statistical analysis of samples in different categories in deep learning classification tasks.
1. Method for drawing line charts
1) Draw a single line
from matplotlib import pyplot as plt
import random
import matplotlib
matplotlib.use('TkAgg')
matplotlib.rcParams['font.sans-serif']=['SimHei']
x = range(0,120)
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#adjustment x the scale of the axis
_xtick_labels = ["10:00 {}".format(i) for i in range(60)]
_xtick_labels += ["11:00 {}".format(i) for i in range(60)]
#take the step size, correspond numbers and strings one by one, and the length of the data is the same
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45) #rotaion the degree of rotation
#add descriptive information
plt.xlabel("time")
plt.ylabel("temperature unit( ℃)")
plt.title("temperature changes per minute from 10:00 to 12:00")
plt.show()
It should be noted that the ticks in xticks should correspond one-to-one with the labels (def xticks (ticks= None, labels= None, * * kwargs)).
2) Draw multiple lines
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
matplotlib.rcParams['font.sans-serif']=['SimHei']
y_1 = [1,0,1,1,2,4,3,2,3,4,4,5]
y_2 = [1,0,3,1,2,2,3,3,2,1 ,2,2]
x = range(1,13)
#set graphic size
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y_1,label="military industry etf",color="#F08080")
plt.plot(x,y_2,label="wine etf",color="#DB7093",linestyle="--")
#set up x axis scale
_xtick_labels = ["{} month".format(i) for i in x]
plt.xticks(x,_xtick_labels)
# plt.yticks(range(0,9))
#draw grid
plt.grid(alpha=0.4,linestyle=':')
#add legend
plt.legend(loc="upper left")
#show
plt.show()

2. Scatter plot drawing method
from matplotlib import pyplot as plt
from matplotlib import font_manager
import matplotlib
matplotlib.use('TkAgg')
matplotlib.rcParams['font.sans-serif']=['SimHei']
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]
x_3 = range(1,32)
x_10 = range(51,82)
#set graphic size
plt.figure(figsize=(20,8),dpi=80)
#apply scatter the only difference between drawing a scatter plot using the method and drawing a line graph before
plt.scatter(x_3,y_3,label="march")
plt.scatter(x_10,y_10,label="october")
#adjustment x the scale of the axis
_x = list(x_3)+list(x_10)
_xtick_labels = ["march {}".format(i) for i in x_3]
_xtick_labels += ["october {}".format(i-50) for i in x_10]
plt.xticks(_x[::3],_xtick_labels[::3],rotation=45)
#add legend
plt.legend(loc="upper left")
#add descriptive information
plt.xlabel("time")
plt.ylabel("temperature")
plt.title("title")
#show
plt.show()

3. Histogram drawing method
import cv2 as cv
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif']=['SimHei']
#reading images
img = cv.imread('wiki.jpg',0)
#open one row and two columns of windows, select window number 1 for operation
plt.subplot(121)
#draw a grayscale image
plt.imshow(img,cmap='gray')
plt.title("picture")
#select window 2
plt.subplot(122)
#draw an image histogram
plt.hist(img.flatten(),256,[0,256], color = 'b',label="histogram")
plt.xlim([0,256])
plt.grid(alpha=0.4)
plt.legend(loc = 'upper right')
plt.show()

4. Bar chart drawing method
Taking reading the German traffic sign classification dataset as an example, count the number of signs in each category.
import csv
from collections import Counter
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
#reading annotation files from the german traffic sign dataset
with open('train_data.csv','r') as f:
reader = csv.reader(f)
column = [row[1] for row in reader]
print(column)
classes = [int(i) for i in column]
print(max(classes))
print(min(classes))
#calculate the quantity of each category
numClasses = Counter(classes)
#set graphic size
plt.figure(figsize=(20,8),dpi=80)
#separate bar charts for each category when the width of the bar chart is smaller than the scale
plt.bar(range(len(numClasses)), list(numClasses.values()), align='center',width=0.8)
plt.xticks(range(len(numClasses)), list(numClasses.keys()))
plt.grid(alpha=0.4)
plt.show()
