当前位置:网站首页>Chapter 6 uses Matplotlib to draw thermodynamic diagram
Chapter 6 uses Matplotlib to draw thermodynamic diagram
2022-04-23 01:34:00 【mr_ songw】
List of articles
Chapter one Use matplotlib Draw line chart
Chapter two Use matplotlib Draw a bar graph
The third chapter Use matplotlib Draw histogram
Chapter four Use matplotlib Draw a scatter plot
The fifth chapter Use matplotlib Draw the pie chart
Chapter six Use matplotlib Draw a heat map
Chapter vii. Use matplotlib Draw a stacked bar chart
Chapter viii. Use matplotlib Draw multiple diagrams in one canvas
List of articles
Preface
Last chapter We talked about the drawing of pie chart , In this chapter, we will talk about the drawing of thermal diagram .
One 、 What is a heat map ?
A heat map is a statistical chart that displays data by coloring color blocks . When drawing , You need to specify the rules for color mapping . for example , Larger values are represented by darker colors , Smaller values are represented by lighter colors ; Larger values are represented by warmer colors , Indicated by the lower value of the color , wait .
Divide... From data structure , Thermodynamic diagrams are generally divided into two types . First of all , Tabular thermodynamic diagram , Also known as color block diagram . It needs to 2 Category fields and 1 A numeric field , The classification field determines x、y Axis , Divide the chart into regular rectangular blocks . The numeric field determines the color of the rectangular block . second , Non tabular thermodynamic diagram , Or a smooth heat map , It needs to 3 A numeric field , Can be drawn in parallel coordinate system (2 The two numeric fields are determined separately x、y Axis ,1 A numeric field determines the color ).
The heat map is suitable for viewing the overall situation 、 Abnormal values were found 、 Show the difference between multiple variables , And whether there is any correlation between them .
Two 、 Drawing of thermal diagram
Let's talk about the drawing of thermal diagram through examples , The sample code is as follows :
import matplotlib.pyplot as plt
import numpy as np
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.imshow(harvest)
plt.tight_layout()
plt.show()
In the code above , Pass a two-dimensional array into imshow In this method, a thermal diagram can be drawn , The color of each color block represents the size of the data . The graphics obtained after code execution are shown in the figure below :
It's just a color block , There is no indication that x Axis and y Meaning of axis , Let's add x Axis and y The label of the shaft , And add a title . The sample code is as follows :
import matplotlib.pyplot as plt
import numpy as np
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.xticks(np.arange(len(farmers)), labels=farmers,
rotation=45, rotation_mode="anchor", ha="right")
plt.yticks(np.arange(len(vegetables)), labels=vegetables)
plt.title("Harvest of local farmers (in tons/year)")
plt.imshow(harvest)
plt.tight_layout()
plt.show()
In the above code ,x The axis represents the name of the farmer ,y The axis represents the name of the vegetable , The data in the two-dimensional array represents the yield of a certain kind of vegetables planted by a farmer . The graphics obtained after code execution are shown in the figure below :
Above picture , We added x Axis and y The meaning of the axis and the title of the whole figure , But now we don't know the size of the corresponding values of different color patches , Now let's add . The sample code is as follows :
import matplotlib.pyplot as plt
import numpy as np
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.xticks(np.arange(len(farmers)), labels=farmers,
rotation=45, rotation_mode="anchor", ha="right")
plt.yticks(np.arange(len(vegetables)), labels=vegetables)
plt.title("Harvest of local farmers (in tons/year)")
plt.imshow(harvest)
plt.colorbar()
plt.tight_layout()
plt.show()
In the above code , We call colorbar Function to add the corresponding rules of values and colors . The graphics obtained after code execution are shown in the figure below :
In the figure above, we add the corresponding rules of color and value . Next, we add the value represented by each color block . for example :
import matplotlib.pyplot as plt
import numpy as np
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.xticks(np.arange(len(farmers)), labels=farmers,
rotation=45, rotation_mode="anchor", ha="right")
plt.yticks(np.arange(len(vegetables)), labels=vegetables)
plt.title("Harvest of local farmers (in tons/year)")
for i in range(len(vegetables)):
for j in range(len(farmers)):
text = plt.text(j, i, harvest[i, j], ha="center", va="center", color="w")
plt.imshow(harvest)
plt.colorbar()
plt.tight_layout()
plt.show()
In the above code , We add the corresponding value to each color block through a cycle . The graphics obtained after code execution are shown in the figure below :
3、 ... and 、 Application scenarios
1. Applicable scenario
- The advantage of a heat map is “ High space utilization ”, It can accommodate relatively large data . Thermal maps not only help to find the relationship between data 、 Find the extreme value , It is also commonly used to describe the overall appearance of data , Facilitate comparison between data sets ( For example, the results of each athlete over the years are condensed into a heat map , Then compare ).
- If you set a row or column as a time variable , The thermodynamic diagram can also be used to show the change of data over time . for example , Use the heat map to reflect the temperature change of a city in a year , The cold and warm trend of the climate , Be clear at a glance .
2. Not applicable to the scene
Although the heat map can accommodate more data , But on the contrary , It is difficult to convert the color blocks into accurate numbers . therefore , When you need to know the value clearly , Additional annotations may be required .
summary
In this chapter, we describe the drawing of thermal map and the applicable and inapplicable scenarios of thermal map .
Last chapter Use matplotlib Draw the pie chart
版权声明
本文为[mr_ songw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230126010302.html
边栏推荐
- Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)
- Counting garlic customers: Sudoku (DFS)
- CVPR | 2022 | expressed by transformer learning multiple hypotheses! A new framework for 3D human pose estimation!
- 修改数组(并查集)
- Prince saves Princess (DFS)
- gin--hello
- What is October 24th? Why are there no senior programmers in China in their fifties and sixties, while foreigners,,, Yu Nianyu Hui take you to celebrate the 1024 programmer Festival
- GBASE 8s并发控制之封锁类型
- Rotation slice
- Small example of gin - get request 1-handle handles get requests
猜你喜欢
Completely uninstall antidote 10? What if the antidote uninstall is not clean?
DFS parity pruning
DFS奇偶性剪枝
计蒜客:方程的解数
The most easy to understand service container and scope of dependency injection
Project manager's thinking mode worth trying: project success equation
代码实现发邮件---sendemails
Unrelated interprocess communication -- creation and use of named pipes
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
How to introduce SPI into a project
随机推荐
Chris LATTNER, father of llvm: the golden age of compilers
ai2022新功能,illustrator 2022 新功能介绍
Find number (DFS)
Basic operation of Android local database | multi thread operation database | addition, deletion, modification and query of database | batch insertion into database | basic use of thread pool | Yu nia
世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
gin -get请求的小示例2-Handle处理post请求
Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)
Here's the point. Have you mastered the most complete Web3 jargon guide?
Counting garlic customers: Sudoku (DFS)
计蒜客:数独(DFS)
. net (c) MySQL conn.open() reports an error: solution to SSL connection error
2n皇后问题
Introduction to gbase 8s checkpoint
[interview skills] how to face an interview without a leading group
GBase 8s 备份介绍
Linked list dynamic header insertion
Rotation slice
Learning of gin framework -- golang
After ten years of testing experience, I have sorted out the most suitable software testing learning guide for you
[the first contact between Android engineers and smart home products ③] the specific implementation of smartconfig one key distribution network on the hardware side | the specific implementation of es