How to Label patch in matplotlib - python

I am plotting rectangular patches in matplotlib in interactive mode. I want to add text to each patch. I do not want to annotate them as it decreases the speed. I am using 'label' property of patch but it is not working. Ayone know how to add 1 string to patch.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
plt.ion()
plt.show()
x = y = 0.1
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
patch = ax1.add_patch(patches.Rectangle((x, y), 0.5, 0.5,
alpha=0.1,facecolor='red',label='Label'))
plt.pause(0)
plt.close()

You already know where the patch is, so you can calculate where the center is and add some text there:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
x=y=0.1
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
patch= ax1.add_patch(patches.Rectangle((x, y), 0.5, 0.5,
alpha=0.1,facecolor='red',label='Label'))
centerx = centery = x + 0.5/2 # obviously use a different formula for different shapes
plt.text(centerx, centery,'lalala')
plt.show()
The coordinates for plt.text determine where the text begins, so you can nudge it a bit in the x direction to get the text to be more centered e.g. centerx - 0.05. obviously #JoeKington's suggestion is the proper way of achieving this

Related

Python - setting arbitrary contour xy-ratio

I am reading the following discussion:
setting axis scale in matplotlib contour plot
From the discussion above, to get arbitrary ratio, we could use
plt.figure(figsize=(8,2))
# ...
plt.tight_layout()
However, this setting is for figure not for contourf.
I used the above codes in my codes
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import pandas as pd
import math
rm = pd.read_excel("test_3d.xlsx", header = None)
# find min values of noise
rec = np.shape(rm)
# grid
X = np.arange(1,rec[1]+1,1)
Y = np.arange(1,rec[0]+1,1)
x , y = np.meshgrid(X,Y)
# plots
plt.clf()
con = plt.contourf(x,y,rm, cmap=cm.jet)
plt.figure(figsize=(8,2))
plt.tight_layout()
plt.title('2457MHz')
plt.show()
The result I got is
The ratio of bottom plot is what I want; however, I use plt.figure(figsize=(8,2)), which is not for contourf. Therefore, I did not get the correct result.
Is there any way that I can plot arbitrary ratio for contourf?
Instead of setting the figsize, use Axes.set_aspect to change the aspect ratio of the contour plot's Axes:
fig, ax = plt.subplots()
ax.contourf(x, y, rm, cmap='viridis')
ax.set_aspect(0.25)
If you prefer to stick with the plt syntax, access the Axes using plt.gca:
plt.contourf(x, y, rm, cmap='viridis')
plt.gca().set_aspect(0.25)

Set transparency (alpha) of matplotlib 3d grid

I would like to change the transparency of the grid in matplotlib 3d plot.
But I find that it is not as easy as in 2d, which is simply plt.grid(alpha=0.2).
Here I give a mini code
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(3, 100)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(data[0], data[1], data[2])
# How to change the grid transparency?
plt.show()
How to set the transparency of the x,y,z-grids?
I have tried:
Using ax.zaxis._axinfo['grid'].update({"alpha": 0.1}). But it appears that it does not have the key alpha.
I checked the source code of ax.grid() here in github. From the comments, it seems that the alpha functionality is not implemented for 3d case at all.
plt.grid does not seem to do anything for 3d plots. But you can set the color as a RGB+Alpha tuple using rcparams:
import numpy as np
import matplotlib.pyplot as plt
# fourth parameter is alpha=0.1
plt.rcParams['grid.color'] = (0.5, 0.5, 0.5, 0.1)
data = np.random.randn(3, 100)
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.scatter(data[0], data[1], data[2])
plt.show()
Result:

Using Colormap with Annotate Arrow in Matplotlib

I have seen many examples of using annotate arrows in Matplotlib that have a single color specified. I was wondering if it is possible to instead set the color according to a colormap, so that the whole range of colors from a specified colormap is displayed on a single arrow. I know that it is possible to set the color of an arrow to a single color from a colormap, but I want to have a single arrow displaying all of the colors of a given colormap.
A simple example of using an annotate arrow is shown below. In the documentation, I have not found any method for specifying a colormap. If I naively specify a colormap, I get an error from an invalid RGBA argument.
import matplotlib.pyplot as plt
RdPu = plt.get_cmap('RdPu')
ax = plt.subplot(111)
ax.annotate("Test", xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
size=20, arrowprops=dict(color=RdPu),
)
plt.show()
Ok, let's produce The Rainbow Arrow. ;-)
There is of course no built-in way to colorize an arrow with a color gradient. Instead one needs to build the arrow manually. I can think of two options. (1) Create a color gradient and clip it with the circonference path of an arrow. (2) Produce a LineCollection with a colorgradient and then add an arrow head to it.
The following is the second option:
import matplotlib.pyplot as plt
import matplotlib.transforms
import matplotlib.path
import numpy as np
from matplotlib.collections import LineCollection
def rainbowarrow(ax, start, end, cmap="viridis", n=50,lw=3):
cmap = plt.get_cmap(cmap,n)
# Arrow shaft: LineCollection
x = np.linspace(start[0],end[0],n)
y = np.linspace(start[1],end[1],n)
points = np.array([x,y]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]], axis=1)
lc = LineCollection(segments, cmap=cmap, linewidth=lw)
lc.set_array(np.linspace(0,1,n))
ax.add_collection(lc)
# Arrow head: Triangle
tricoords = [(0,-0.4),(0.5,0),(0,0.4),(0,-0.4)]
angle = np.arctan2(end[1]-start[1],end[0]-start[0])
rot = matplotlib.transforms.Affine2D().rotate(angle)
tricoords2 = rot.transform(tricoords)
tri = matplotlib.path.Path(tricoords2, closed=True)
ax.scatter(end[0],end[1], c=1, s=(2*lw)**2, marker=tri, cmap=cmap,vmin=0)
ax.autoscale_view()
fig,ax = plt.subplots()
ax.axis([0,5,0,4])
ax.set_aspect("equal")
rainbowarrow(ax, (3,3), (2,2.5), cmap="viridis", n=100,lw=3)
rainbowarrow(ax, (1,1), (1.5,1.5), cmap="jet", n=50,lw=7)
rainbowarrow(ax, (4,1.3), (2.7,1.0), cmap="RdYlBu", n=23,lw=5)
plt.show()
The following is the old solution, caused by a misunderstanding
An annotation arrow is a single arrow. Hence you would need to draw any number of arrows individually. In order for each arrow to then obtain a color, you may use the arrowprops=dict(color="<some color>") argument.
To get colors from a colormap, you can call the colormap with a value. Here the length of the arrow can be taken as the quantity to encode as color.
import matplotlib.pyplot as plt
import numpy as np
RdPu = plt.get_cmap('RdPu')
ax = plt.subplot(111)
ax.axis([-6,2,-4.5,3.2])
ax.set_aspect("equal")
X = np.linspace(0,1,17, endpoint=False)
Xt =np.sin(2.5*X+3)
Yt = 3*np.cos(2.6*X+3.4)
Xh = np.linspace(-0.5,-5,17)
Yh = -1.3*Xh-5
#Distance
D = np.sqrt((Xh-Xt)**2+(Yh-Yt)**2)
norm = plt.Normalize(D.min(), D.max())
for xt, yt, xh, yh, d in zip(Xt,Yt,Xh,Yh,D):
ax.annotate("Test", xy=(xh,yh), xycoords='data',
xytext=(xt,yt), textcoords='data',
size=10, arrowprops=dict(color=RdPu(norm(d))))
plt.show()

How can I embed an image on each of my subplots in matplotlib?

I'm trying to put a little arrow in the corner of each of my subplots. Below is the sample code I'm using:
import matplotlib.pyplot as plt
import matplotlib.image as image
from numpy import linspace
xs = linspace(0, 1, 100)
im = image.imread('arrow.png')
def multi_plot():
fig, axes = plt.subplots(4, 1)
x = 0
for axis in axes:
axis.plot(xs, xs**2)
axis.imshow(im, extent=(0.4, 0.6, .5, .7), zorder=-1, aspect='auto')
plt.show()
multi_plot()
Unfortunately, this produces 4 subplots that are entirely dominated by the arrows and the plots themselves are not seen.
Example output - Incorrect:
What do I need to do so that each individual subplot has a small image and the plot itself can be seen?
I think it's worthwhile thinking about putting the image in a box and place it similar to the legend, using a loc argument. The advantage is that you don't need to care about extents and data coordinates at all. You also wouldn't need to take care of what happens when zooming or panning the plot. Further it allows to keep the image in it's original resolution (zoom=1 in below code).
import matplotlib.pyplot as plt
import matplotlib.image as image
from numpy import linspace
from matplotlib.offsetbox import OffsetImage,AnchoredOffsetbox
xs = linspace(0, 1, 100)
im = image.imread('arrow.png')
def place_image(im, loc=3, ax=None, zoom=1, **kw):
if ax==None: ax=plt.gca()
imagebox = OffsetImage(im, zoom=zoom*0.72)
ab = AnchoredOffsetbox(loc=loc, child=imagebox, frameon=False, **kw)
ax.add_artist(ab)
def multi_plot():
fig, axes = plt.subplots(4, 1)
for axis in axes:
axis.plot(xs, xs**2)
place_image(im, loc=2, ax=axis, pad=0, zoom=1)
plt.show()
multi_plot()
You'll notice that the limits on the x and y axis have been set to the extent of the imshow, rather than 0-1, which your plot needs to see the line.
You can control this by using axis.set_xlim(0, 1) and axis.set_ylim(0, 1).
import matplotlib.pyplot as plt
import matplotlib.image as image
from numpy import linspace
xs = linspace(0, 1, 100)
im = image.imread('arrow.png')
def multi_plot():
fig, axes = plt.subplots(4, 1)
x = 0
for axis in axes:
axis.plot(xs, xs**2)
axis.imshow(im, extent=(0.4, 0.6, .5, .7), zorder=-1, aspect='auto')
axis.set_xlim(0, 1)
axis.set_ylim(0, 1)
plt.show()
multi_plot()
Alternatively, if you want to maintain the extra 5% margin around your data that matplotlib uses by default, you can move the imshow command to before the plot command, then the latter will control the axis limits.
import matplotlib.pyplot as plt
import matplotlib.image as image
from numpy import linspace
xs = linspace(0, 1, 100)
im = image.imread('arrow.png')
def multi_plot():
fig, axes = plt.subplots(4, 1)
x = 0
for axis in axes:
axis.imshow(im, extent=(0.4, 0.6, .5, .7), zorder=-1, aspect='auto')
axis.plot(xs, xs**2)
plt.show()
multi_plot()

How to draw a line outside of an axis in matplotlib (in figure coordinates)

Matplotlib has a function that writes text in figure coordinates (.figtext())
Is there a way to do the same but for drawing lines?
In particular my goal is to draw lines to group some ticks on the y-axis together.
Tested in python 3.8.12, matplotlib 3.4.3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
x = np.linspace(0,10,100)
y = np.sin(x)*(1+x)
fig, ax = plt.subplots()
ax.plot(x,y,label='a')
# new clear axis overlay with 0-1 limits
ax2 = plt.axes([0,0,1,1], facecolor=(1,1,1,0))
x,y = np.array([[0.05, 0.1, 0.9], [0.05, 0.5, 0.9]])
line = Line2D(x, y, lw=5., color='r', alpha=0.4)
ax2.add_line(line)
plt.show()
But if you want to align with ticks, then why not use plot coordinates?

Categories