当前位置:网站首页>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
边栏推荐
- Linked list dynamic header insertion
- Text justify, orientation, combine text attributes
- Gbase 8s数据库日志简介及管理
- 轮转法分片
- Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
- GBASE 8s并发控制之封锁类型
- 京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。
- Realize the function of progress bar through layerdrawable
- Function encapsulation such as addition, deletion, modification and query of linked list (summary)
- 学习方法与职业发展指南(2022年版)
猜你喜欢

Level 4 city area table xlsx, SQL file, domestic, provincial, county, street, township level 4 address in China (name, linkage ID, level, end level or not (1-yes))

Small example of gin - get request 2-handle handles post requests

京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。

“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负

彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?

Innovative practice of short video content understanding and generation technology in meituan

引爆炸弹(DFS)

Soatest preliminary understanding

Examples of branch and loop statements

Chris LATTNER, father of llvm: the golden age of compilers
随机推荐
2n queen problem
Redis实现分布式锁
计蒜客:方程的解数
Traversal of ladder race l2-6 tree
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
Custom numeric input control
Itextsharp infrastructure
[course summary] Hello harmonyos series of courses, take you to zero foundation introduction
京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。
Gbase 8s query processing and optimization
iTextSharp 基础结构
engine.POST()处理POST请求
Gbase 8t index
Introduction to gbase8s SQL Engine framework
Blocking granularity of gbase 8s concurrency control
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system
Jisuan Hakka spectrum (DFS for direct post algebra)
Gbase 8s存儲結構簡介及空間管理
王子救公主(DFS)