Plotly: how to fill the background of a contour plot when the axes have been resized?

Question:

I draw a contour plot of two sets of 2d points and wish to show the distribution of each sets side by side using the same scale for the axes.

For plotting the distribution I use go.Histogram2dContour.

The problem is that when I rescale the axes, areas outside (what I assume was) the original contour plots are left bare and show the usual grey on the default background of Plotly plots. The result is a rectangle in the middle of the plot with the image of the contour plot inside it and that grey around it. See image.

Result of the code example

Is there any way to fill the remaining of the plot so that it looks better? Some plot types have the connectgaps=True option to fill in the gaps but this does not seem to be available for the Histogram2dContour type (I tried as a keyword argument of go.Histogram2dContour as well as a value in a contours dictionary argument of that function, none are accepted as valid).

Here is a minimalist version of the code I use:


import numpy as np
np_random = np.random.default_rng()
import plotly.graph_objects as go
from plotly.subplots import make_subplots


fixed_range = 100
scatter_set_1 = np_random.normal(scale=85, size=(164, 2))
scatter_set_2 = np_random.normal(scale=26, size=(78, 2))

fig = make_subplots(1, 2, shared_yaxes=True, subplot_titles=['scatter set 1', 'scatter set 2'])

fig.update_yaxes(range=[-fixed_range, fixed_range])
fig.update_xaxes(range=[-fixed_range, fixed_range])

fig.add_trace(go.Histogram2dContour(x=scatter_set_1[:,0],
                                    y=scatter_set_1[:,1],
                                    colorscale='Reds',
                                    showscale=False), 1, 1)
fig.add_trace(go.Histogram2dContour(x=scatter_set_2[:,0],
                                    y=scatter_set_2[:,1],
                                    colorscale='Greens',
                                    showscale=False), 1, 2)



fig.show()

Asked By: osc-ml

||

Answers:

I think your best option would be:

fig.update_layout(paper_bgcolor='white',
                  plot_bgcolor='white')

Plot

enter image description here

Answered By: vestland
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.