How to draw thermal map with Python
This article mainly introduces "how to draw a heat map with Python". In daily operation, I believe many people have doubts about how to use Python to draw a heat map. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for you to answer the question of "how to use Python to draw a heat map". Next, please follow the editor to study!
Step 1: install and import related packages
Two packages, pandas and pyecharts, are mainly used. Pandas is used to read the data I store in Excel, and pyecharts is used to visualize the data.
You need to pay attention to the version of pyecharts here, because the code may be different from version to version, and I installed the latest version, version 1.7.1.
If you don't have these two packages, install them first:
Pip install pyecharts pip install pandas
Import after installation is complete:
Import pandas as pd from pyecharts import options as opts from pyecharts.charts import HeatMap
Step 2: read the data
My data is put in Excel, as follows:
Student grades. Xlsx
Read data with pandas:
Data = pd.read_excel ('student grades .xlsx') x = data ['name'] .tolist () y = data.columns.values.tolist () values = [[I, j, int (data.iloc [I, juni1])] for i in range (len (x)) for j in range (len (y))]
The last line of code is used to combine each score with its corresponding row and column index (which can also be understood as coordinates).
I know you don't understand this, so I just print out the values as follows:
[[0, 0, 85], [0, 1, 74], [0, 2, 62], [0, 3, 72], [0, 4, 87], [0, 5, 80], [0, 6, 74], [0, 7, 94], [0, 8, 82], [1, 0, 72], [1, 1, 66], [1, 2, 71], [1, 3, 60], [1, 4, 66], [1, 5 68], [1, 6, 68], [1, 7, 62], [1, 8, 93], [2, 0, 96], [2, 1, 80], [2, 2, 75], [2, 3, 85], [2, 4, 70], [2, 5, 83], [2, 6, 90], [2, 7, 75], [2, 8, 83], [3, 0, 78], [3, 1, 61] [3, 2, 70], [3, 3, 78], [3, 4, 89], [3, 5, 70], [3, 6, 65], [3, 7, 76], [3, 8, 95], [4, 0, 65], [4, 1, 81], [4, 2, 72], [4, 3, 96], [4, 4, 77], [4, 5, 88], [4, 6, 78], [4, 7 90], [4, 8, 83], [5, 0, 70], [5, 1, 90], [5, 2, 65], [5, 3, 91], [5, 4, 85], [5, 5, 75], [5, 6, 70], [5, 7, 83], [5, 8, 72], [6, 0, 63], [6, 1, 72], [6, 2, 70], [6, 3, 80] [6, 4, 65], [6, 5, 80], [6, 6, 68], [6, 7, 93], [6, 8, 85]]
Step 3: draw a picture
MyHeatMap = HeatMap () myHeatMap.add_xaxis (x) myHeatMap.add_yaxis ("student grades", y, values) myHeatMap.set_global_opts (title_opts=opts.TitleOpts (title= "HeatMap"), visualmap_opts=opts.VisualMapOpts (min_=60, max_=100) myHeatMap.render (path=' student scores .html')
Well, there should be nothing to say up there, everyone will understand.
Where 60 and 100 represent the range of my data, because all the scores are in the range of 60 to 100.
After running the above code, you will get a html file, which is opened as follows:
At this point, the study on "how to draw a thermal map with Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!