preface
Data provided: data
What is a thermal map?
The thermal map shows the distribution of data by the shade of color.
In the example diagram, the lighter the color, the higher the value, so we can see the distribution of the data at a glance, which is very convenient.
2. Draw a thermal map
Requirements for data: draw the thermal map of order volume in April in E region, with the horizontal axis in days and the vertical axis in hours
- Load the necessary packages
import pandas as pd
import numpy as np
import seaborn as sns # Draw a thermal map for use
import matplotlib.pyplot as plt
Copy the code
- Load the data
path='.. / data/data. CSV '
f=open(path)
data=pd.read_csv(f)
Copy the code
- Place and time of extraction
E_data=data[data['place'] = ='E']
E_data['Time of payment'] = pd.to_datetime(E_data['Time of payment'])
month_data=E_data[E_data['Time of payment'].dt.month==4]
Copy the code
- Calculate order quantity in groups
order_quantity_group=month_data.groupby([month_data['Time of payment'].dt.day,month_data['Time of payment'].dt.hour]).size()
Copy the code
According to grouping by day and hour, size() function represents the number of data, that is, the order quantity. The results are as follows:
As can be seen from the result, the grouping basis given by us is “month of payment time” and “hour” respectively in the format of tuple type in the index.
- Create a new matrix
The thermogram is drawn according to the matrix, so the data obtained are arranged into the appropriate matrix before drawing the thermogram.
m=list(map(list,zip(*list(order_quantity_group.index)))) # Separate tuples containing day and hour data
Create a new matrix
new_array=np.zeros((30.24)) #30 indicates the number of days and 24 indicates the number of hours
for i,j in zip(m[0],m[1) :#m[0] is the month, m[1] is the hour
new_array[i- 1][j]=order_quantity_group[(i,j)]
Copy the code
- Thermal map
sns.heatmap(new_array.T, annot=True,xticklabels= range(1.32), yticklabels= True, cmap="YlGnBu") Draw a thermal map.
plt.title('April Order Volume Heat Map') # title
plt.xlabel("Date") # X axis labels
plt.ylabel("Hour") # Y axis labels
plt.show()
f.close()
Copy the code
SNS. Heatmap is a function to draw a heatmap.
- The first parameter is the matrix data, and since the requirements specify that the horizontal axis is day and the vertical axis is hour, a transpose is required.
- The annot parameter indicates whether to display data in the graph, if True, and otherwise only colors.
- Xticklabels and ytickLabels are a set of arguments that, if True, draw the column names of the data box. If False, column names are not added, and if list type data, these are replaced with X-axis labels.
- Cmap can set colors
As can be seen from the figure, sales in area E are concentrated in the middle of the day and in the afternoon.
Other issues
data
Python drawing Chinese font does not display problems
Data analysis practice – bar chart
Data analysis practice – line chart
Data analysis practice – pie chart
Data analysis – bubble chart
Data analysis actual combat – thermal map
reference
Matplotlib official information
Seaborn official information