Issue
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
Solution
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:
Answered By - K.Cl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.