python繪制雙變量熱力等級圖

參考資料:
https://github.com/mikhailsirenko/bivariate-choropleth/blob/main/bivariate-choropleth.ipynb
Bivariate choropleth map using Plotly

Matplotlib雙變量熱力等級圖

代碼:

import pandas as pd
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from adjustText import adjust_text
import contextily as ctx
from sklearn.preprocessing import MinMaxScaler
from scipy.stats.mstats import winsorizedata = gpd.read_file('data.json')
data.head()
border = gpd.read_file('border.json')# Define the bins
bins = [0, 0.33, 0.66, 1]# Bin the first variable - x
data['Var1_Class'] = pd.cut(data['x'], bins=bins, include_lowest=True)
data['Var1_Class'] = data['Var1_Class'].astype('str')# Bin the second variable - y
data['Var2_Class'] = pd.cut(data['y'], bins=bins, include_lowest=True)
data['Var2_Class'] = data['Var2_Class'].astype('str')# Code created x bins to 1, 2, 3
x_class_codes = np.arange(1, len(bins))
d = dict(zip(data['Var1_Class'].value_counts().sort_index().index, x_class_codes))
data['Var1_Class'] = data['Var1_Class'].replace(d)# Code created y bins to A, B, C
y_class_codes = ['A', 'B', 'C']
d = dict(zip(data['Var2_Class'].value_counts().sort_index().index, y_class_codes))
data['Var2_Class'] = data['Var2_Class'].replace(d)# Combine x and y codes to create Bi_Class
data['Bi_Class'] = data['Var1_Class'].astype('str') + data['Var2_Class']all_colors = ['#e8e8e8', '#b0d5df', '#64acbe', '#e4acac', '#ad9ea5', '#627f8c', '#c85a5a', '#985356', '#574249']
colors = ['#e8e8e8', # 1A'#b0d5df', # 1B# '#64acbe', # 1C'#e4acac', # 2A'#ad9ea5', # 2B# '#627f8c', # 2C'#c85a5a', # 3A'#985356'] # 3B# '#574249'] # 3Ccmap = matplotlib.colors.ListedColormap(colors)
fig, ax = plt.subplots(figsize=(8,8))# Step 1: Draw the map
border.to_crs('EPSG:3857').plot(ax=ax, facecolor='none', edgecolor='black', alpha=.5) # city border
data.to_crs('EPSG:3857').plot(ax=ax, column='Bi_Class', # variable that is going to be used to color the map cmap=cmap, # newly defined bivariate cmap categorical=True, # bivariate choropleth has to be colored as categorical map legend=False) # we're going to draw the legend ourselves
ctx.add_basemap(ax=ax, source=ctx.providers.CartoDB.Positron) # add the basemapplt.tight_layout() # "tighten" two figures map and basemap
plt.axis('off') # we don't need axis with coordinates
ax.set_title('Bivariate Choropleth Amsterdam')# Step 2: draw the legend# We're drawing a 3x3 "box" as 3 columns
# The xmin and xmax arguments axvspan are defined to create equally sized small boxesimg2 = fig # refer to the main figure
ax2 = fig.add_axes([0.15, 0.25, 0.1, 0.1]) # add new axes to place the legend there# and specify its location 
alpha = 1 # alpha argument to make it more/less transperent# Column 1
ax2.axvspan(xmin=0, xmax=0.33, ymin=0, ymax=0.33, alpha=alpha, color=all_colors[0])
ax2.axvspan(xmin=0, xmax=0.33, ymin=0.33, ymax=0.66, alpha=alpha, color=all_colors[1])
ax2.axvspan(xmin=0, xmax=0.33, ymin=0.66, ymax=1, alpha=alpha, color=all_colors[2])# Column 2
ax2.axvspan(xmin=0.33, xmax=0.66, ymin=0, ymax=0.33, alpha=alpha, color=all_colors[3])
ax2.axvspan(xmin=0.33, xmax=0.66, ymin=0.33, ymax=0.66, alpha=alpha, color=all_colors[4])
ax2.axvspan(xmin=0.33, xmax=0.66, ymin=0.66, ymax=1, alpha=alpha, color=all_colors[5])# Column 3
ax2.axvspan(xmin=0.66, xmax=1, ymin=0, ymax=0.33, alpha=alpha, color=all_colors[6])
ax2.axvspan(xmin=0.66, xmax=1, ymin=0.33, ymax=0.66, alpha=alpha, color=all_colors[7])
ax2.axvspan(xmin=0.66, xmax=1, ymin=0.66, ymax=1, alpha=alpha, color=all_colors[8])# Step 3: annoate the legend
ax2.tick_params(axis='both', which='both', length=0) # remove ticks from the big box
ax2.axis('off'); # turn off its axis
ax2.annotate("", xy=(0, 1), xytext=(0, 0), arrowprops=dict(arrowstyle="->", lw=1)) # draw arrow for x 
ax2.annotate("", xy=(1, 0), xytext=(0, 0), arrowprops=dict(arrowstyle="->", lw=1)) # draw arrow for y 
ax2.text(s='Var1\_Class', x=0.1, y=-0.25) # annotate x axis
ax2.text(s='Var2\_Class', x=-0.25, y=0.1, rotation=90); # annotate y axis
plt.savefig('bivariate_choropleth.png', dpi=300)

代碼來源:https://github.com/mikhailsirenko/bivariate-choropleth/blob/main/bivariate-choropleth.ipynb

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/22264.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/22264.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/22264.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

企業轉型必上的監控系統智能管家大屏UI前端開發

企業轉型必上的監控系統智能管家大屏UI前端開發

Istio安裝記錄

環境介紹 我使用的是k8s 1.23.3版本 istio使用的是istio-1.13.3-linux-amd64.tar.gz 把文件下載k8s集群下,解壓 tar -vzxf istio-1.13.3-linux-amd64.tar.gz然后設置環境變量 [rootmaster istio]# cat /etc/profile export ISTIO_HOME/root/istio-1.13.3 expor…

3067. 在帶權樹網絡中統計可連接服務器對數目 Medium

給你一棵無根帶權樹,樹中總共有 n 個節點,分別表示 n 個服務器,服務器從 0 到 n - 1 編號。同時給你一個數組 edges ,其中 edges[i] [ai, bi, weighti] 表示節點 ai 和 bi 之間有一條雙向邊,邊的權值為 weighti 。再給…

Yolo-v5模型訓練速度,與GeForce的AI算力描述

1.GeForce RTX3070 Ti官網參數: GeForce RTXTM 3070 Ti 和 RTX 3070 顯卡采用第 2 代 NVIDIA RTX 架構 - NVIDIA Ampere 架構。該系列產品搭載專用的第 2 代 RT Core ,第 3 代 Tensor Core、全新的 SM 多單元流處理器以及高速顯存,助您在高性…

【網絡安全的神秘世界】MySQL

🌝博客主頁:泥菩薩 💖專欄:Linux探索之旅 | 網絡安全的神秘世界 | 專接本 MySQL MySQL 教程 | 菜鳥教程 (runoob.com) 什么是數據庫 數據庫(Database)是按照數據結構來組織、存儲和管理數據的倉庫 在do…

二手筆記本怎么買

用途: 1.給爹媽用來簡單辦公,只是用office基礎辦公軟件,無出差無游戲無畫圖需求。 預算: 1000以內 以下是電腦對比選項: 屏幕大小-> 目前市面上的尺寸對比,以A4紙說明,13.3寸14.1寸15.6…

Camunda 7.x 系列【66】實戰篇之我發起的

有道無術,術尚可求,有術無道,止于術。 本系列Spring Boot 版本 2.7.9 本系列Camunda 版本 7.19.0 源碼地址:https://gitee.com/pearl-organization/camunda-study-demo 前后端基于若依:https://gitee.com/y_project/RuoYi-Vue 流程設計器基于RuoYi-flowable:https://gi…

參數高效微調PEFT(四)快速入門(IA)3

參數高效微調PEFT(四)快速入門(IA)3 我們已經了解了HuggingFace中peft庫的幾種高效微調方法。 參數高效微調PEFT(一)快速入門BitFit、Prompt Tuning、Prefix Tuning 參數高效微調PEFT(二)快速入門P-Tuning、P-Tuning V2 參數高效微調PEFT(三)快速入門LoRA、AdaLoRA 今天我…

探索 Omost:創新的圖像生成AI框架

文章目錄 探索 Omost:創新的圖像生成AI框架第一部分:背景第二部分:Omost是什么?第三部分:如何安裝Omost?第四部分:結合具體場景使用第五部分:總結 探索 Omost:創新的圖像…

OceanBase 4.3 特性解析:列存技術

在涉及大規模數據的復雜分析或即時查詢時,列式存儲是支撐業務負載的關鍵技術之一。相較于傳統的行式存儲,列式存儲采用了不同的數據文件組織方式,它將表中的數據以列為單位進行物理排列。這種存儲模式允許在分析過程中,查詢計算僅…

flowable工作流 完成任務代碼 及擴展節點審核人(實現多級部門主管 審核等)詳解【JAVA+springboot】

低代碼項目 使用flowable 工作流 完成任務代碼 詳解 可以看到 complete()方法 傳遞了流程變量參數var 前端傳遞此參數就可以實現 流程中 審批 更新流程變量參數var 也可以進行更多擴展 實現流程中更新表單內容功能 啟動流程實例代碼 實現對于流程自定義 動態節點審核人 功…

中央空調節能的分戶計費系統

中央空調節能 在建筑能耗中,中央空調能耗一般占到了40%---60%的比例,因此如何有效降低空調能耗就成為建筑節能的重中之重。 項目案例描述 山東銀座購物廣場:為集購物中心、高級酒店式公寓和辦公為一體的綜合性公共建筑。整體建筑共為地下3層&…

副業變現:Midjourney繪畫賺錢的6種方式

今年被稱為AI元年,其中最火的兩款AI工具非ChatGpt和Midjourney莫屬。究其原因,無非兩點:第一,它提高了生產力,之前需要兩年完成的工作,使用ChatGpt兩天就完成。 第二,它帶來了副業收入&#x…

JavaScript異步編程簡單介紹

JavaScript異步編程是一種編程模式,用于處理需要等待某些操作完成之后才能繼續執行的代碼。這些操作可以是網絡請求、文件讀取、定時器等等。 異步編程的目標是避免阻塞代碼執行,在等待操作完成的同時,允許其他代碼繼續執行。 以下是一個使…

Springboot-RabbitMQ 消息隊列使用

一、概念介紹: RabbitMQ中幾個重要的概念介紹: Channels:信道,多路復用連接中的一條獨立的雙向數據流通道。信道是建立在真實的 TCP 連接內地虛擬連接,AMQP 命令都是通過信道發出去的,不管是發布消息、訂閱…

2021 hnust 湖科大 數字系統設計與VHDL課程 大作業 - 出租車計價器設計

2021 hnust 湖科大 數字系統設計與VHDL課程大作業-出租車計價器設計 描述 大二上的eda考查課的實驗,額外實現了停車等待2分鐘后收費1元/min。內含項目文件(實測可運行),代碼,報告,視頻和照片,…

JavaScript函數定義,函數參數,函數調用

JavaScript函數定義: 在JavaScript中,我們可以使用關鍵字function來定義一個函數。函數定義的一般語法如下: function functionName(parameter1, parameter2, ...){// 函數體 }其中,functionName是函數的名稱,可以自定…

功能強大且專業的PDF轉換軟件PDF Shaper Professional 14.2

PDF Shaper Professional是一款適用于Windows的程序,可讓您在計算機上處理PDF文件。 要開始使用PDF Shaper Professional,您需要在Windows計算機上下載并安裝該程序。您還應該有合適的驅動程序和編解碼器來處理計算機上的文本和圖形。 安裝程序后&#…

分享一份糟糕透頂的簡歷,看看跟你寫的一樣不

最近看了一個人的簡歷,怎么說呢,前幾年這么寫沒問題,投出去就有回復,但從現在開始,這么寫肯定不行了。下面我給大家分享一下內容: 目錄 🤦?♀?這是簡歷文檔截圖 🤷?♀?這是基本…

淘寶評論API調用指南,讓你購物不再困擾

一、淘寶評論API概述 淘寶評論API是淘寶開放平臺提供的一種服務,它允許開發者通過調用API接口獲取淘寶商品評論數據,聯訊數據從而為用戶提供更加豐富和實用的購物決策信息。通過使用淘寶評論API,開發者可以輕松地實現以下功能: …