I assume this has to do with misusing the API and axis.transData
, but wanted to check if there is anything obvious to fix here: My goal is to
- have a bunch of circular markers
- have them just touch at the edges
Here is a stripped down version of the function:
import matplotlib.pyplot as plt
import numpy as np
def funny_stuff(axis=None):
np.random.seed(1)
centers = np.arange(0, 10, step=np.abs(np.random.randn()))
if axis is None:
axis = plt.gca()
circs = axis.scatter(centers, np.ones_like(centers))
bin_width = centers[1] - centers[0]
axis.set_xlim(centers[0] - bin_width, centers[-1] + bin_width)
axis.set_ylim(0, 2)
diff = axis.transData.transform([(0, 1), (bin_width, 0), (0, 0)])
x_scale = max(diff[0] - diff[-1])
y_scale = max(diff[1] - diff[-1])
if x_scale > y_scale:
axis.set_ylim(0, (x_scale / y_scale) * 2)
diff = axis.transData.transform([(0, 1), (0, 0)])
size = max(diff[0] - diff[-1])
circs.set_sizes([size ** 2 for _ in centers])
return axis
If I run this in a jupyter notebook, it works as desired:
If I instead run this in a script (using either plt.show
or savefig
), I get something different (I am a new user, and can include only one image ), and the circles are overlapping.
I have tried setting backends, and playing with arguments to savefig
, but have had no luck. If I call savefig
from the notebook, it works as desired.