#🔒 Extract contour coordinates

9 messages · Page 1 of 1 (latest)

karmic prism
#

Hello everyone,

I've come to ask you for a service that I don't know how to do. It's something quite complicated for me, being inexperienced in the field.

I'm using a library called "corner" to display 1D (or 2D) histograms. I use it particularly for drawing contours. These contours correspond to density contours because, initially, the data correspond to simple points on the graph (mass on radius). The aim is to calculate the density of these points and define contours of a certain density.

I've managed to display these contours, but I want to extract the coordinates of the various contours, and that's where I get totally stuck.

Here's an overview of the graph and the code, maybe it'll help you and I'd like to thank you in advance for any help you can give me.

import numpy as np
import matplotlib.pyplot as plt
import corner

data = np.loadtxt('[a file with mass and radius]')

masse = data[:, 0]
rayon = data[:, 1]

bin_size = [0.1, 0.05]

num_bins_masse = int((max(masse) - min(masse)) / bin_size[0])
num_bins_rayon = int((max(rayon) - min(rayon)) / bin_size[1])

contour_colors = ['red', 'green', 'blue']

hist, x_edges, y_edges = np.histogram2d(masse, rayon, bins=[num_bins_masse, num_bins_rayon])

# /!\ Proba density 
total_points = np.sum(hist)
probabilities = hist / total_points

figure = corner.hist2d(masse, rayon, contour_kwargs={'colors': contour_colors}, levels=[0.68, 0.95, 0.9999], bins=[num_bins_masse, num_bins_rayon])

plt.xlabel('Rayon (km)')
plt.ylabel('Masse (Msun)')
plt.title('Contours de densité des échantillons MCMC')

# Afficher le plot
plt.show()
drifting knollBOT
#

@karmic prism

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

warped birch
drifting knollBOT
#

src/corner/core.py line 520

def hist2d(```
warped birch
drifting knollBOT
#

src/corner/core.py line 725

# Extend the array for the sake of the contours at the plot edges.```
karmic prism
#

Thanks for your help, I'll admit I'm at the same point as you for all these methods but I'll look into it, thanks again!

drifting knollBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.