Reputation: 4596
Consider the following toy code:
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
def draw_circle_arrangement(ax, drawing_origin, drawing_space, scale, num_circles, box_height, box_width):
bw = drawing_space*(box_width*scale)
drawing_origin[0] = drawing_origin[0] - bw*0.5
circle_diameter = drawing_space*scale
circle_radius = 0.5*circle_diameter
y_delta = np.array([0., circle_diameter])
x_delta = np.array([circle_diameter, 0.])
cell_origin = drawing_origin + np.array([circle_radius, circle_radius])
y_delta_index = 0
x_delta_index = 0
for ci in range(num_circles):
cell_patch = mpatches.Circle(cell_origin + y_delta_index*y_delta + x_delta_index*x_delta, radius=circle_radius, color='k', fill=False, ls='solid', clip_on=False)
ax.add_artist(cell_patch)
if y_delta_index == box_height - 1:
y_delta_index = 0
x_delta_index += 1
else:
y_delta_index += 1
fig, ax = plt.subplots()
# each tuple is: number of circles, height of box containing circles, width of box containing circle
circle_arrangements = [(10, 2, 5), (3, 1, 3), (1, 1, 1)]
data = np.random.rand(3)
ax.set_ylim([0, 1])
ax.plot(np.arange(3) + 1, data, marker='o')
ax.get_xaxis().set_ticklabels([])
scale = 1./10.
for i, ca in enumerate(circle_arrangements):
do = np.array([1.0 + i, -0.2])
nc, bh, bw = ca
draw_circle_arrangement(ax, do, 0.8, scale, nc, bh, bw)
When run, it produces output like so:
As you can see, the circles are not circles! They are squished. One way to fix this is by setting ax.set_aspect('equal')
, but if I don't necessarily want the axis aspect ratio to be equal, how can I still have the patches produced that way?
Upvotes: 2
Views: 366
Reputation: 339170
It is not possible to only set part of the axes aspect to equal. The solution would be either to draw the circles in a different coordinate system, or, by using a different box that uses a different coordinate system.
I find the latter solution preferable. That would involve using a DrawingArea
which can be placed inside an AnnotationBbox
.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.offsetbox import DrawingArea, AnnotationBbox
def draw_circle_arrangement(ax, drawing_origin, radius,
num_circles, box_height, box_width):
y_delta_index = 0
x_delta_index = 0
origin = np.array([radius,radius])
y_delta = np.array([0., 2*radius])
x_delta = np.array([2*radius, 0.])
da = DrawingArea(box_width*2*radius, box_height*2*radius, 0, 0)
for ci in range(num_circles):
cell_patch = mpatches.Circle(origin+y_delta_index*y_delta + x_delta_index*x_delta,
radius=radius, color='k', fill=False, ls='solid',clip_on=False)
da.add_artist(cell_patch)
if y_delta_index == box_height - 1:
y_delta_index = 0
x_delta_index += 1
else:
y_delta_index += 1
ab = AnnotationBbox(da, xy=(drawing_origin[0],0),
xybox=drawing_origin,
xycoords=("data", "axes fraction"),
boxcoords=("data", "axes fraction"),
box_alignment=(0.5,0.5), frameon=False)
ax.add_artist(ab)
fig, ax = plt.subplots()
# each tuple is: number of circles, height of box containing circles, width of
#box containing circle
circle_arrangements = [(10, 2, 5), (3, 1, 3), (1, 1, 1)]
data = np.random.rand(3)
ax.set_ylim([0, 1])
ax.plot(np.arange(3) + 1, data, marker='o')
ax.get_xaxis().set_ticklabels([])
for i, ca in enumerate(circle_arrangements):
do = np.array([1.0 + i, -0.06])
nc, bh, bw = ca
draw_circle_arrangement(ax, do, 5, nc, bh, bw)
plt.show()
The units of the DrawingArea
are points. E.g. in the first case we create a DrawingArea of 5*2*5 = 50 points width and place 5 circles with a radius of 5 points into it (the 5 circles fill the complete 50 points then). A point is ~1.4 pixels. The position of the AnnotationBox is given in data coordinates for the x position and in axes fraction for the y position. This can be changed by using the boxcoords
argument. Same for the xy
argument, where we make sure that the y coordinate is at 0, and thus inside the axes (such that it AnnotationsBox is shown).
Upvotes: 3