Python Plot меняет цвета на подграфике — как привязать один цвет к одному набору данных?Python

Программы на Python
Ответить
Anonymous
 Python Plot меняет цвета на подграфике — как привязать один цвет к одному набору данных?

Сообщение Anonymous »

Я пытаюсь построить график некоторых данных из прогона Монте-Карло цепи Маркова, используя getDist, который (я предполагаю) использует matplotlib под капотом для построения графика. Нет никакого сообщения об ошибке или чего-то еще, и он создает почти правильный график, за исключением одного важного момента: подграфики 0 и 3 имеют противоположный порядок цветов, чем подграфик 2, т. е. в подграфиках 0 и 3 набор данных 1 темно-синий, а набор данных 2 — желтый, в то время как в подграфике 2 набор данных 1 — желтый, а набор данных 2 — темно-синий. Есть ли способ избежать этого, например, назначить фиксированный цвет набору данных?
Код следующий:

Код: Выделить всё

# Import required libraries:
# - matplotlib.pyplot for plotting
# - cmcrameri.cm for perceptually uniform colormaps
# - getdist.plots for MCMC chain plotting
import matplotlib.pyplot as plt
from cmcrameri import cm
from getdist import plots

# Initialize the getdist subplot plotter
# - chain_dir: directory where MCMC chain files are stored
# - analysis_settings: ignore_rows to skip burn-in (first 20% of samples)
g = plots.get_subplot_plotter(
chain_dir=r"/Users/klmba", analysis_settings={"ignore_rows": 0.2}
)

# Define the root names of the MCMC chains (file prefixes without extensions)
roots = [
"Cobaya_mcmc_Run3_Planck_PP_SH0ES_DESIDR2_DoubleExp_tracking_uncoupled",
"cobaya_iDM_20251230_dexp",
]

# Specify the parameters to plot in the triangle plot
params = ["H0", "s8h5"]  # Hubble constant and S8

# Extract a list of colors from the categorical bamako colourmap for coloring the chains
colours = [tuple(c) for c in cm.bamakoS.colors]

# Configure the plotter settings
# - solid_colors: set the colors used for filled contours and lines
g.settings.solid_colors = colours

# Generate the triangle plot showing:
# - 1D marginalized distributions on the diagonal
# - 2D contour plots on the off-diagonal
g.triangle_plot(
roots,  # List of chain roots to include
params,  # Parameters to plot
filled=True,  # Fill the contour regions
colors=colours,  # Colors for contour lines and fills
contour_lws=3,  # Line width for contours
legend_loc="lower left",  # Legend position
figure_legend_outside=True,  # Place legend outside the plot area
)

# Add reference bands for observational data
# SH0ES 2020b measurement of H0: 73.2 ± 1.3 km/s/Mpc
g.add_x_bands(73.2, 1.3, ax=0)  # Vertical band on H0 1D plot
g.add_x_bands(73.2, 1.3, ax=2)  # Vertical band on H0 axis of 2D plot

# KiDS-1000 2023 measurement of S8: 0.776 ± 0.031
g.add_x_bands(0.776, 0.031, ax=3)  # Horizontal band on S8 1D plot
g.add_y_bands(0.776, 0.031, ax=2)  # Horizontal band on S8 axis of 2D plot

# Export the plot to default file format (usually PDF/PNG)
g.export()

# Display the plot in the output
plt.show()
Он создает следующие графики:
Изображение


Подробнее здесь: https://stackoverflow.com/questions/798 ... o-one-data
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»