matplotlib: ordering of zoomed axes objects - python

I am building a figure with a primary axis that is a scatter plot and a zoomed axis which focuses on a particular region of the primary axis, both of which have gridlines. When I place the zoomed axis as an inset, it "covers" up some of the primary axis data. I want to be able to show the primary axis data (zorder=100) through the zoomed axis, so I set the zoomed axis to be transparent (alpha=0). Finally, I want the primary axis gridlines to "cut-off" when they meet the zoomed axis (zorder=10) but I want to show the zoomed axis gridlines (zorder=50). Is this possible? Below is my attempt:
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
import numpy as np
fig = plt.figure(figsize=(10,7.5))
gs = matplotlib.gridspec.GridSpec(1, 2, width_ratios=[20,1], height_ratios=[1])
ax = plt.subplot(111)
## data
xx = np.linspace(1,100,num=100) + 20 * np.random.normal(0,1,100)
yy = np.linspace(1,100,num=100) + 10 * np.random.normal(0,1,100)
## scatter
sc = ax.scatter(xx, yy, s=250, alpha=0.35, zorder=100)
ax.plot(np.linspace(-100,200,301), np.linspace(-100,200,301),)
ax.set_xlim((0, 100))
ax.set_ylim((0, 100))
ax.grid(linestyle="--", zorder=10)
## zoom
axins = zoomed_inset_axes(ax, 2, loc="upper left")
scins = axins.scatter(xx, yy, s=100, alpha=0.35, zorder=50, marker=".", c="red")
axins.plot(np.linspace(-100,200,301), np.linspace(-100,200,301), c="red")
axins.set_xlim((70, 90))
axins.set_ylim((70, 90))
mark_inset(ax, axins, loc1=1, loc2=4, fc="none", ec="0.5")
axins.grid(linestyle="--", zorder=50)
plt.show()
In particular, one of the blue data points near x=80 gets cut off. I can set axins.patch.set_alpha(0.0), but then it doesn't remove the primary grid lines.

One option is indeed to put a white patch (white rectangle) in ax at the position where axins lives and set that patches' zorder to higher than the one from the gridlines, but lower than the one from the scatter.
# Set axins' background patch invisible
axins.patch.set_visible(False)
# Create a new patch at the position of the axins axes.
rect = matplotlib.patches.Rectangle((0,0), 1,1,
fill=True, facecolor="white", edgecolor="red",zorder=25,
transform=axins.transAxes)
ax.add_patch(rect)

Thanks to #ImportanceOfBeingErnest for the suggestion. It works to add a rectangle with an intermediate zorder to ax per the following (where I've left the red outline of the rectangle):
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
import matplotlib.patches as patches
import numpy as np
## data
xx = np.linspace(1,100,num=100) + 20 * np.random.normal(0,1,100)
yy = np.linspace(1,100,num=100) + 10 * np.random.normal(0,1,100)
## fig
fig = plt.figure(figsize=(10,7.5))
gs = matplotlib.gridspec.GridSpec(1, 2, width_ratios=[20,1], height_ratios=[1])
ax = plt.subplot(111)
## scatter
sc = ax.scatter(xx, yy, s=250, alpha=0.35, zorder=100)
ax.plot(np.linspace(-100,200,301), np.linspace(-100,200,301))
ax.set_xlim((0, 100))
ax.set_ylim((0, 100))
ax.grid(linestyle="--", zorder=10)
ax.patches.extend([patches.Rectangle((0.2, 0.6), 0.4, 0.4,
fill=True, facecolor="white", edgecolor="red",zorder=25,
transform=ax.transAxes, figure=ax)])
## zoom
axins = zoomed_inset_axes(ax, 2,
bbox_to_anchor=(0.6, 1.0, 0.0, 0.0),
bbox_transform=ax.transAxes)
scins = axins.scatter(xx, yy, s=100, alpha=0.35, zorder=50, marker=".", c="red")
axins.plot(np.linspace(-100,200,301), np.linspace(-100,200,301), c="red")
axins.set_xlim((70, 90))
axins.set_ylim((70, 90))
axins.patch.set_alpha(0.0)
mark_inset(ax, axins, loc1=1, loc2=4, fc="none", ec="0.5")
axins.grid(linestyle="--", zorder=50)
plt.show()

Related

How to add axis label (x and y) and rotate y axis numbers with Matplotlib

How to add axis label (x and y) and rotate y axis numbers with Matplotlib like on the image below ?
I tried plt.yticks(rotation=45) to rotate the y axis numbers but it's not taken into account.
Besides, I'm also trying to have one 0 instead of two in my example code and a square grid instead of rectangles.
from mpl_toolkits.axisartist.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ["xzero", "yzero"]:
# adds arrows at the ends of each axis
ax.axis[direction].set_axisline_style('->')
# adds X and Y-axis from the origin
ax.axis[direction].set_visible(True)
ax.axis['yzero'].set_ticklabel_direction("-")
for direction in ["left", "right", "bottom", "top"]:
# hides borders
ax.axis[direction].set_visible(False)
x = np.linspace(-5, 5, 100)
ax.plot(x, -x**2+16, color="#ab74a6", linewidth=3)
plt.title(r'$y = -x^2+16$')
plt.yticks(rotation=45)
plt.axis([-5, 5, -10, 20])
plt.grid(True)
plt.show()
Here's a working code example using spines rather than SubplotZero:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
x = np.linspace(-5, 5, 100)
ax.plot(x, -x**2+16, color="#ab74a6", linewidth=3)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# hide one of the zero labels and adjust the other
ax.yaxis.get_major_ticks()[3].label1.set_visible(False)
ax.xaxis.get_major_ticks()[3].label1.set_horizontalalignment("right")
ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False)
ax.axis('equal')
ax.set_xlabel('x', position=(1,0), ha='right')
ax.set_ylabel('y', position=(0,1), ha='right', rotation=0)
plt.title(r'$y = -x^2+16$', y=1.08)
plt.grid(True)
plt.show()

Colorbar for inset in python

I am trying to add an additional small colorbar for the inset axis. The current code, without that, is
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.colors as colors
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
A = np.linspace(1,20,20)
B = A
X,Y = np.meshgrid(A,B)
Z = X**2 + Y**2
fig, ax = plt.subplots()
im = ax.pcolor(X, Y, Z, cmap='hot_r')
ax.set_xlabel('x',fontsize=labelsize)
ax.set_ylabel('y',fontsize=labelsize)
ca = fig.colorbar(im)#, shrink=0.5, aspect=5)
axins = ax.inset_axes([0.1, 0.5, 0.25, 0.25])
axins.pcolor(A[0:4], B[0:4], Z[0:4,0:4], cmap='hot_r')
axins.tick_params(axis='both', which='major', labelsize=11)
for axis in ['top','bottom','left','right']:
axins.spines[axis].set_linewidth(1)
axins.spines[axis].set_color('gray')
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec='gray', lw=1)
plt.tight_layout()
You could create an additional inset axis for the colorbar. E.g. located just right of the inset. Then create a colorbar proving this axis (cax=...).
Please note that pcolor creates faces (large pixels) between the given x and y positions. So, you need one row and one column more of position then the number of colors. The current version of matplotlib gives a warning in case too many colors (or not enough x and y positions) are given.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
A = np.linspace(1, 20, 20)
B = A
X, Y = np.meshgrid(A, B)
Z = X ** 2 + Y ** 2
fig, ax = plt.subplots()
im = ax.pcolor(X, Y, Z[:-1, :-1], cmap='hot_r')
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ca = fig.colorbar(im) # , shrink=0.5, aspect=5)
axins = ax.inset_axes([0.1, 0.5, 0.25, 0.25])
axins_cbar = ax.inset_axes([0.37, 0.5, 0.02, 0.25])
img_in = axins.pcolor(A[0:5], B[0:5], Z[0:4, 0:4], cmap='hot_r')
axins.tick_params(axis='both', which='major', labelsize=11)
for axis in ['top', 'bottom', 'left', 'right']:
axins.spines[axis].set_linewidth(1)
axins.spines[axis].set_color('gray')
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec='gray', lw=1)
fig.colorbar(img_in, cax=axins_cbar)
plt.tight_layout()
plt.show()

How to rotate a Subplot by 45 degree in Matplotlib?

I am trying to explore a subplot 2 plots with square in shape rotated by 45 degree.
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
data = np.random.rand(10, 10) * 20
# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue','green'])
bounds = [0,5,10,15]
norm = colors.BoundaryNorm(bounds, cmap.N)
fig, ax= plt.subplots(1,2)
ax[0].imshow(data, cmap=cmap, norm=norm)
# draw gridlines
ax[0].grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
ax[0].set_xticks(np.arange(-.5, 10, 1));
ax[0].set_yticks(np.arange(-.5, 10, 1));
ax[1].imshow(data, cmap=cmap, norm=norm)
# draw gridlines
ax[1].grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
ax[1].set_xticks(np.arange(-.5, 10, 1));
ax[1].set_yticks(np.arange(-.5, 10, 1));
plt.show()
Actual Result is :-
I want to rotate individual plot by 45 degree. Something like:-
I am trying to find in Matplotlib Documentation. Still not getting. Any help?
Please note this is NOT DUPLICATE OF
Is there a way to rotate a matplotlib plot by 45 degrees?
The mentioned URL is for a plot. and the solution is to rotate IMAGE. However this is pertaining to Subplot. I want to rotate PLOT not image as whole.
Based on this link and documentation about floating_axes, you can try something like this:
from mpl_toolkits.axisartist.grid_finder import DictFormatter
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
from matplotlib import colors
import numpy as np
def setup_axes1(fig, rect, angle):
tr = Affine2D().scale(2, 2).rotate_deg(angle)
#We create dictionarys to keep the xticks and yticks after the rotation
dictio={i:str(val) for i,val in enumerate(np.arange(-.5, 10, 1).tolist())}
reversedictio={i:dictio[val] for i,val in enumerate(list(reversed(sorted(dictio.keys()))))}
grid_helper = floating_axes.GridHelperCurveLinear(
tr, extremes=(-0.5, 9.5,-0.5, 9.5), tick_formatter1= DictFormatter(dictio),
tick_formatter2=DictFormatter(reversedictio))
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
aux_ax = ax1.get_aux_axes(tr)
grid_helper.grid_finder.grid_locator1._nbins = 10 #Number of rows
grid_helper.grid_finder.grid_locator2._nbins = 10 #Number of columns
return aux_ax
fig1, axes=plt.subplots(2,figsize=(20,20))
plt.rcParams.update({'font.size': 27})
#We erase the first previous axes
fig1.delaxes(axes[0])
fig1.delaxes(axes[1])
data = np.random.rand(10, 10) * 20
#We create the floating_axes
ax0 = setup_axes1(fig1, 121,-45)
ax1 = setup_axes1(fig1, 122,-45)
# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue','green'])
bounds = [0,5,10,15]
norm = colors.BoundaryNorm(bounds, cmap.N)
ax0.imshow(data, cmap=cmap, norm=norm,interpolation="nearest")
# draw gridlines
ax0.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
ax1.imshow(data, cmap=cmap, norm=norm,interpolation="nearest")
# draw gridlines
ax1.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
plt.show()
Output:
Or, as an other alternative, I found a "tricky" way to do it, and it's about catching the figures in the buffer, rotate them -45 degrees, and then merge them into a single image, and since you have the same two images, you can try something like this:
import matplotlib
import io
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
##PLOTING THE FIGURE##
data = np.random.rand(10, 10) * 20
# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue','green'])
bounds = [0,5,10,15]
norm = colors.BoundaryNorm(bounds, cmap.N)
#We change style values to get the image with better quality
plt.rcParams.update({'font.size': 46})
plt.figure(figsize=(20,20))
plt.imshow(data, cmap=cmap, norm=norm)
# draw gridlines
plt.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
plt.gca().set_xticks(np.arange(-.5, 10, 1));
plt.gca().set_yticks(np.arange(-.5, 10, 1));
##SAVING THE FIGURE INTO AN IMAGE##
#We save the current figure as a Image
buf = io.BytesIO()
plt.savefig(buf, format='png',bbox_inches='tight')
buf.seek(0)
im = Image.open(buf) #We open the current image saved in the buffer
#We rotate the image and fill the background with white
img_01=im.rotate(-45, Image.NEAREST, expand = 1, fillcolor = (255,255,255))
buf.close()
##MERGING THE TWO FIGURES##
new_im = Image.new('RGB', (2*img_01.size[0]+20,img_01.size[1]), 'white')
mouse_mask = img_01.convert('RGBA')
new_im.paste(img_01, (0,0))
new_im.paste(img_01, (img_01.size[0]+8,0))
new_im.save("merged_images.png", 'PNG') #Important(just to clarify): save the image, since the buffer is renewed every time you run the script
new_im.show()
Output:
I helped myself with these links:
How-to-merge-images-with-same-size-using-the-python-3-module-pillow
how-to-save-a-pylab-figure-into-in-memory-file-which-can-be-read-into-pil-image
python-pillow-rotate-image-90-180-270-degrees
specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand

How to plot multiple subplots over a background image with matplotlib?

I want to plot some scatter plots over the map of a country (an image). The idea is to depict the data visualization of the area at which the plot is plotted.
So, this is how I plot the image of the map of USA, where the circles I have drawn towards the top left and the middle are where I would like to display my scatter plots:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,15))
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im, extent=[0, 200, 0, 150])
# A circle in the upper left region
theta=np.linspace(0,2*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 45
t=np.sin(theta)*faux_radius + 130
plt.plot(z,t)
# A circle in the middle region
theta=np.linspace(0,3*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 100
t=np.sin(theta)*faux_radius + 80
plt.plot(z,t)
This plots the image like so:
I proceed to plot the scatter plots like so:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,15))
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im, extent=[0, 200, 0, 150])
# A circle in the upper left region
theta=np.linspace(0,2*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 45
t=np.sin(theta)*faux_radius + 130
plt.plot(z,t)
# A circle in the middle region
theta=np.linspace(0,3*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 100
t=np.sin(theta)*faux_radius + 80
plt.plot(z,t)
# Scatter plot 1
ax1 = plt.subplot(2,2,1)
ax1.scatter(x_1_a, y_1_a, marker="s")
ax1.scatter(x_1_b, y_1_b, marker="o")
# Scatter plot 2
ax1 = plt.subplot(2,2,2)
ax1.scatter(x_2_a, y_2_a, marker="s")
ax1.scatter(x_2_a, y_2_b, marker="o")
But the output it produces does not display the background image, and only plots the scatter plots:
[]
I even tried using zorder which is supposed to tell matplotlib which plot should come on top and which on bottom, but to no avail - it produces the same output as above:
implot = plt.imshow(im, extent=[0, 200, 0, 150], zorder=1)
...
...
...
# Scatter plot 1
ax1 = plt.subplot(2,2,1)
ax1.scatter(x_1_a, y_1_a, marker="s", zorder=2)
ax1.scatter(x_1_b, y_1_b, marker="o", zorder=2)
# Scatter plot 2
ax1 = plt.subplot(2,2,2)
ax1.scatter(x_2_a, y_2_a, marker="s", zorder=3)
ax1.scatter(x_2_a, y_2_b, marker="o", zorder=3)
How do I fix this to get the desired result? I don't even need the 2 circles to be present on the map actually - those were just to illustrate where I would like to plot the 2 scatter plots. Thanks.
I was able to solve the problem using the plt.axes suggestion in the comments:
from mpl_toolkits.axes_grid.inset_locator import inset_axes
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10, 15),facecolor='white')
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im)
plt.xticks([])
plt.yticks([])
# this is an inset axes over the main axes for the top left region
a = plt.axes([.2, .6, .2, .1], facecolor='w')
plt.scatter(x_1_a, y_1_a, marker="s")
plt.scatter(x_1_b, y_1_b, marker="o")
plt.legend(['%.2f%%' %(100*len(x_1_a)/(len(x_1_a)+len(y_1_a))), '%.2f%%' %(100*len(y_1_a)/(len(x_1_a)+len(y_1_a)))], loc='upper right');
# this is an inset axes over the main axes for the middle region
a = plt.axes([.45, .45, .2, .1], facecolor='w')
plt.scatter(x_2_a, y_2_a, marker="s")
plt.scatter(x_2_b, y_2_b, marker="o")
plt.legend(['%.2f%%' %(100*len(x_2_b)/(len(x_2_b)+len(y_2_b))), '%.2f%%' %(100*len(y_2_b)/(len(x_2_b)+len(y_2_b)))], loc='upper right');
plt.show()

Make border of Label, bbox or axes.text flush with spines of Graph in python matplotlib

for a certain manuscript i need to position my label of the Graph exactly in the right or left top corner. The label needs a border with the same thickness as the spines of the graph. Currently i do it like this:
import matplotlib.pyplot as plt
import numpy as np
my_dpi=96
xposr_box=0.975
ypos_box=0.94
nrows=3
Mytext="label"
GLOBAL_LINEWIDTH=2
fig, axes = plt.subplots(nrows=nrows, sharex=True, sharey=True, figsize=
(380/my_dpi, 400/my_dpi), dpi=my_dpi)
fig.subplots_adjust(hspace=0.0001)
colors = ('k', 'r', 'b')
for ax, color in zip(axes, colors):
data = np.random.random(1) * np.random.random(10)
ax.plot(data, marker='o', linestyle='none', color=color)
for ax in ['top','bottom','left','right']:
for idata in range(0,nrows):
axes[idata].spines[ax].set_linewidth(GLOBAL_LINEWIDTH)
axes[0].text(xposr_box, ypos_box , Mytext, color='black',fontsize=8,
horizontalalignment='right',verticalalignment='top', transform=axes[0].transAxes,
bbox=dict(facecolor='white', edgecolor='black',linewidth=GLOBAL_LINEWIDTH))
plt.savefig("Label_test.png",format='png', dpi=600,transparent=True)
So i control the position of the box with the parameters:
xposr_box=0.975
ypos_box=0.94
If i change the width of my plot, the position of my box also changes, but it should always have the top and right ( or left) spine directly on top of the graphs spines:
import matplotlib.pyplot as plt
import numpy as np
my_dpi=96
xposr_box=0.975
ypos_box=0.94
nrows=3
Mytext="label"
GLOBAL_LINEWIDTH=2
fig, axes = plt.subplots(nrows=nrows, sharex=True, sharey=True, figsize=
(500/my_dpi, 400/my_dpi), dpi=my_dpi)
fig.subplots_adjust(hspace=0.0001)
colors = ('k', 'r', 'b')
for ax, color in zip(axes, colors):
data = np.random.random(1) * np.random.random(10)
ax.plot(data, marker='o', linestyle='none', color=color)
for ax in ['top','bottom','left','right']:
for idata in range(0,nrows):
axes[idata].spines[ax].set_linewidth(GLOBAL_LINEWIDTH)
axes[0].text(xposr_box, ypos_box , Mytext, color='black',fontsize=8,
horizontalalignment='right',verticalalignment='top',transform=axes[0].transAxes,
bbox=dict(facecolor='white', edgecolor='black',linewidth=GLOBAL_LINEWIDTH))
plt.savefig("Label_test.png",format='png', dpi=600,transparent=True)
This should also be the case if the image is narrower not wider as in this example.I would like to avoid doing this manually. Is there a way to always position it where it should? Independent on the width and height of the plot
and the amount of stacked Graphs?
The problem is that the position of a text element is relative to the text's extent, not to its surrounding box. While it would in principle be possible to calculate the border padding and position the text such that it sits at coordinates (1,1)-borderpadding, this is rather cumbersome since (1,1) is in axes coordinates and borderpadding in figure points.
There is however a nice alternative, using matplotlib.offsetbox.AnchoredText. This creates a textbox which can be placed easily relative the the axes, using the location parameters like a legend, e.g. loc="upper right". Using a zero padding around that text box directly places it on top of the axes spines.
from matplotlib.offsetbox import AnchoredText
txt = AnchoredText("text", loc="upper right", pad=0.4, borderpad=0, )
ax.add_artist(txt)
A complete example:
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText
import numpy as np
my_dpi=96
nrows=3
Mytext="label"
plt.rcParams["axes.linewidth"] = 2
plt.rcParams["patch.linewidth"] = 2
fig, axes = plt.subplots(nrows=nrows, sharex=True, sharey=True, figsize=
(500./my_dpi, 400./my_dpi), dpi=my_dpi)
fig.subplots_adjust(hspace=0.0001)
colors = ('k', 'r', 'b')
for ax, color in zip(axes, colors):
data = np.random.random(1) * np.random.random(10)
ax.plot(data, marker='o', linestyle='none', color=color)
txt = AnchoredText(Mytext, loc="upper right",
pad=0.4, borderpad=0, prop={"fontsize":8})
axes[0].add_artist(txt)
plt.show()
In principle you can align text to the Axes spines using Annotations and position them in Axes coordinates (x and y between 0 and 1) using xycoords = 'axes fraction. However, because you use a bbox that bbox will overlap with the spines.
Instead, you can use ax.text together a ScaledTransformation, which, if done right, also positions the text in Axes coordinates and shifts it by a fixed amount. If you provide a pad size to the bbox keyword, you know exactly how much the bbox will overlap with the spines in figure points (1 inch is 72 points), so that the shift is easily calculated. Here a little demonstration how to do this:
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.transforms as transforms
GLOBAL_LINEWIDTH=2
pad = 10
fig,ax = plt.subplots()
x = np.linspace(0,1,20)
ax.plot(x,x**2, 'ro')
offset_bl = transforms.ScaledTranslation(
pad/72, pad/72, fig.dpi_scale_trans,
)
offset_br = transforms.ScaledTranslation(
-pad/72, pad/72, fig.dpi_scale_trans,
)
offset_tl = transforms.ScaledTranslation(
pad/72, -pad/72, fig.dpi_scale_trans,
)
offset_tr = transforms.ScaledTranslation(
-pad/72, -pad/72, fig.dpi_scale_trans,
)
for pos in ['top','bottom','left','right']:
ax.spines[pos].set_linewidth(GLOBAL_LINEWIDTH)
ax.text(
0,0, 'bottom left',
fontsize = 16, fontweight='bold', va='bottom', ha='left',
bbox=dict(
facecolor = 'white', edgecolor='black', lw = GLOBAL_LINEWIDTH,
pad = pad
),
transform=ax.transAxes + offset_bl,
)
ax.text(
1,0, 'bottom right',
fontsize = 16, fontweight='bold', va='bottom', ha='right',
bbox=dict(
facecolor = 'white', edgecolor='black', lw = GLOBAL_LINEWIDTH,
pad = pad
),
transform=ax.transAxes + offset_br,
)
ax.text(
0,1, 'top left',
fontsize = 16, fontweight='bold', va='top', ha='left',
bbox=dict(
facecolor = 'white', edgecolor='black', lw = GLOBAL_LINEWIDTH,
pad = pad
),
transform=ax.transAxes + offset_tl,
)
ax.text(
1,1, 'top right',
fontsize = 16, fontweight='bold', va='top', ha='right',
bbox=dict(
facecolor = 'white', edgecolor='black', lw = GLOBAL_LINEWIDTH,
pad = pad
),
transform=ax.transAxes + offset_tr,
)
plt.show()
And here is the result:

Categories