Adding rectangles to a plot - doesn't show the right dimension [duplicate] - python

I'm trying to draw a rectangle in matplotlib using the following code:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
width = 20
height = 10
rect = patches.Rectangle((0,0),width, height, linewidth=4,edgecolor='r',facecolor='none')
ax.add_patch(rect)
plt.show()
Which results in:
The axes do not fit the rectangle limits in this case. I could solve it with:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
width = 20
height = 10
ax.set_xlim(0,width)
ax.set_ylim(0,height)
rect = patches.Rectangle((0,0),width, height, linewidth=4,edgecolor='r',facecolor='none')
ax.add_patch(rect)
plt.show()
This gives me the following picture which solves the problem in this case:
However, as I am trying to plot many rectangles and other shapes in the same figure, I need a way that matplotlib smartly determines the proper axes limits itself, like the way it does when plotting normal diagrams.

You are looking for .autoscale(). You may use .margins(0) to remove any extra space that is added by default.
I.e.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
width = 20
height = 10
rect = patches.Rectangle((0,0),width, height, linewidth=4,edgecolor='r',facecolor='none')
ax.add_patch(rect)
ax.margins(0)
ax.autoscale()
plt.show()

Related

How can I use SymLogNorm with matplotlib but still have colorbar appear linear?

Is there a way to use SymLogNorm with imshow, but make the colorbar basically stretch the colors so that the colorbar actually appears linear?
Below is a short code
from pylab import *
import numpy as np
from matplotlib.colors import SymLogNorm
data = np.random.uniform(low=-10, high=10, size=(10,10))
norm = SymLogNorm(2,vmin=-10,vmax=10)
fig, axes = plt.subplots()
im = axes.imshow(data,extent=[-10,10,-10,10],cmap=plt.cm.jet,norm=norm)
cb = fig.colorbar(im)
that produces this
I basically want this image, but want to stretch the colorbar so the ticks appear linear, not log.

Matplotlib - Aligning grids

I'm plotting histograms below the images using a Matplotlib.GridSpec as we can see on code below:
import imageio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
plt.close('all')
plt.style.use('ggplot')
img = imageio.imread('imageio:page.png')
y = np.bincount(img.ravel(), minlength=256)
y = y/np.sum(y)
x = np.arange(len(y))
fig = plt.figure(figsize=(6,8))
gs = gridspec.GridSpec(2, 1, height_ratios=[6,1], width_ratios=[1])
ax0 = plt.subplot(gs[0])
ax0.imshow(img, cmap='gray')
ax0.xaxis.set_visible(False)
ax0.yaxis.set_visible(False)
ax1 = plt.subplot(gs[1])
ax1.fill_between(x, y)
ax1.yaxis.set_visible(False)
ax1.set_xlim([0,255])
fig.tight_layout()
plt.show()
When we pick the correct figure size the image is nicely aligned as in
But if the figure size isn't correctly chosen the histogram is shown too large for image size or too far away as we can see below
or
Is there any way to tell matplotlib to align correctly, that is, put the histogram a fixed amount of pixels below the image and never stretch the histogram larger than image width.

Way to change only the width of marker in scatterplot but not height?

I want to make a scatterplot with marker type as rectange (not square), such that width is more than height. With the "s" I can control the overall size of the marker but it increases in both dimension.
I can not directly pass height and width as these are unknown properties of scatter.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker='s', s=16)
Try the following snippet.
import numpy as np
import matplotlib.pyplot as plt
width = 60
height = 30
verts = list(zip([-width,width,width,-width],[-height,-height,height,height]))
fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker=(verts,0),s=40)
Here, the argument s changes the size of the scatter. The drawn rectangle keeps the ratio width/height.
Output:
update
Since matplotlib 3.2x, use of (verts, 0) is depreciated. The working code should be changed to
fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker=verts, s=40)

Python & Matplotlib: how to plot ellipse?

I can plot ellipse like this:
from matplotlib.patches import Ellipse
import matplotlib as mpl
%matplotlib inline
from matplotlib import pyplot as plt
mean = [ 19.92977907 , 5.07380955]
width = 30
height = 1.01828848
angle = -54
ell = mpl.patches.Ellipse(xy=mean, width=width, height=height, angle = 180+angle)
fig, ax = plt.subplots()
ax.add_artist(ell)
ax.set_aspect('equal')
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
plt.show()
However, this requires me to set the axis data limits manually. Can it be set automatically? I mean, how to get rid of ax.set_xlim(-100, 100) and ax.set_ylim(-100, 100)?
Or, what is a good way of plotting ellipse?
You need to add the patch using add_patch, not add_artist, then the data limits will be updated properly using ax.autoscale:
from matplotlib.patches import Ellipse
import matplotlib as mpl
%matplotlib inline
from matplotlib import pyplot as plt
mean = [ 19.92977907 , 5.07380955]
width = 30
height = 1.01828848
angle = -54
ell = mpl.patches.Ellipse(xy=mean, width=width, height=height, angle = 180+angle)
fig, ax = plt.subplots()
ax.add_patch(ell)
ax.set_aspect('equal')
ax.autoscale()
plt.show()

How do I draw a rectangle on the legend in matplotlib?

I am trying to plot a rectangle onto the legend in matplotlib.
To illustrate how far I have gotten I show my best attempt, which does NOT work:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
Fig = plt.figure()
ax = plt.subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax.plot(t, s1, 'b-', label = 'dots')
leg = ax.legend()
rectangle = Rectangle((leg.get_frame().get_x(),
leg.get_frame().get_y()),
leg.get_frame().get_width(),
leg.get_frame().get_height(),
fc = 'red'
)
ax.add_patch(rectangle)
plt.show()
The rectangle just isn't draw anywhere in the figure.
If I look at the values of leg.get_frame().get_x(), leg.get_frame().get_y()), leg.get_frame().get_width() and leg.get_frame().get_height(), I see that they are
0.0, 0.0, 1.0 and 1.0 respectively.
My problem thus sees to be, to find the co-ordinates of the frame of the legend.
It would be really great if you could help me out.
Thanks for reading this far.
This link may have the exact thing you are looking for.
http://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])
plt.show()
The trouble is that the position of the legend is not known in advance. Only by the time you render the figure (calling plot()), is the position decided.
A solution I came across is to draw the figure twice. In addition, I've used axes coordinates (default is data coordinates) and scaled the rectangle so you still see a bit of the legend behind it. Note that I had to set the legend and rectangle zorder as well; the legend gets drawn later than the rectangle and thus the rectangle otherwise disappears behind the legend.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
Fig = plt.figure()
ax = plt.subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax.plot(t, s1, 'b-', label = 'dots')
leg = ax.legend()
leg.set_zorder(1)
plt.draw() # legend position is now known
bbox = leg.legendPatch.get_bbox().inverse_transformed(ax.transAxes)
rectangle = Rectangle((bbox.x0, bbox.y0),
bbox.width*0.8, bbox.height*0.8,
fc='red', transform=ax.transAxes, zorder=2)
ax.add_patch(rectangle)
plt.show()

Categories

Resources