简体   繁体   中英

How can I get the x and y coordinate values for the region with the highest count / "hottest" in a Hexbin plot?

I'm using a line symmetry detector for a project I've found on github and it makes use of matplotlib's hexbin plot to identify coordinates to find the symmetric line.

Unfortunately, this method is very manual and requires the user to identify the x and y coordinates through the generated plot, and then input the values into the program again.

Is there a way to return the x and y values where the region is hottest in the hexbin plot?

For reference, this is the generated hexbin plot . The coordinates I am looking for is roughly x=153.0 y=1.535

plt.hexbin returns a PolyCollection object, which has the X and Y positions and their values, see here . You can access them with get_offsets() and get_array() . Here's an example:

# Figure from https://www.geeksforgeeks.org/matplotlib-pyplot-hexbin-function-in-python/
import matplotlib.pyplot as plt 
import numpy as np 
    
np.random.seed(19680801) 
    
n = 100000
x = np.random.standard_normal(n) 
y = 12 * np.random.standard_normal(n) 
     
polycollection = plt.hexbin(x, y, gridsize = 50, cmap ='Greens') 
# New part
max_ = polycollection.get_array().max()
max_pos = polycollection.get_array().argmax()
pos_x, pos_y = polycollection.get_offsets()[max_pos]
plt.text(pos_x, pos_y, max_, color='w')

This is the resulting plot:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM