How to set labels with MATPLOTLIB? - python

I have my image
Then I try this
#!/usr/bin/python
import os,sys
import Image
import matplotlib.pyplot as plt
jpgfile = Image.open("t002.jpg")
fig = plt.imshow(jpgfile)
ax = fig.add_subplot(111)
ax.set_xlabel('normlized resistivities')
ay.set_ylabel('normlized velocities')
fig.savefig("fig.jpg")
But then I have
AttributeError: 'AxesImage' object has no attribute 'add_subplot'
How to setup xlabel and ylabel and then save new image as a file?

It should be enough to simply do
plt.figure()
plt.imshow(jpgfile)
plt.xlabel('normlized resistivities')
plt.ylabel('normlized velocities')
plt.savefig('out.jpg')
Your current error is because imshow does not return a Figure.

I guess the best way is to actually plot the image, and only show the axis labels (hide axis and ticks)
This reply may be the way to go

You could just use plt.xlabel('xlabel')

Hannes's answer works fine, but sometimes you need the ax object, so I tend to do this sort of thing:
import os,sys
import Image
import matplotlib.pyplot as plt
jpgfile = Image.open("t002.jpg")
# Set up the figure and axes.
fig = plt.figure(figsize=(12,8)) # ...or whatever size you want.
ax = fig.add_subplot(111)
# Draw things.
plt.imshow(jpgfile)
ax.set_xlabel('normalized resistivities')
ax.set_ylabel('normalized velocities')
# Save and show.
plt.savefig("fig.jpg")
plt.show()
By the way, I recommend saving as PNG for figures containing text and other elements with fine detail. And setting a high dpi for the saved figure.

Related

How to rasterize a color bar axis spines in Matplotlib?

I am generating a large PDF with numerous pages. I trying to rasterize everything I can within it to save some disk space. However, the color bars are particularly tough to deal with.
Can someone manage to rasterize the color bar axis spines in the example below?
import matplotlib.pyplot as plt
import numpy as np
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
cb = plt.colorbar(im)
ax.set_rasterized(True)
plt.suptitle('Title')
plt.savefig('test.pdf')
As it is now, when zooming in, the axes of the image look pixelated but not the axes of the color bar. I want both to be pixelated.
I thought im.colorbar.solids.set_rasterized(True) would make the trick but it did not. Saving the image as a PNG file is not an option since I want to keep the figure title not rasterized (this is the only thing I do not want rasterized)
It turns out it is necessary to access the axis of the color bar directly. The for loop below makes the trick
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure() # <--- New line
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
cb = plt.colorbar(im)
for ax in fig.get_axes(): # <--- New line
ax.set_rasterized(True) # <--- New line
plt.suptitle('Title')
plt.savefig('test.pdf')

Add my own picture as marker in animation in Python [duplicate]

I would like to utilize customer markers in both scatter and line charts. How can I make custom marker out of a PNG file?
I don't believe matplotlib can customize markers like that. See here for the level of customization, which falls way short of what you need.
As an alternative, I've coded up this kludge which uses matplotlib.image to place images at the line point locations.
import matplotlib.pyplot as plt
from matplotlib import image
# constant
dpi = 72
path = 'smile.png'
# read in our png file
im = image.imread(path)
image_size = im.shape[1], im.shape[0]
fig = plt.figure(dpi=dpi)
ax = fig.add_subplot(111)
# plot our line with transparent markers, and markersize the size of our image
line, = ax.plot((1,2,3,4),(1,2,3,4),"bo",mfc="None",mec="None",markersize=image_size[0] * (dpi/ 96))
# we need to make the frame transparent so the image can be seen
# only in trunk can you put the image on top of the plot, see this link:
# http://www.mail-archive.com/matplotlib-users#lists.sourceforge.net/msg14534.html
ax.patch.set_alpha(0)
ax.set_xlim((0,5))
ax.set_ylim((0,5))
# translate point positions to pixel positions
# figimage needs pixels not points
line._transform_path()
path, affine = line._transformed_path.get_transformed_points_and_affine()
path = affine.transform_path(path)
for pixelPoint in path.vertices:
# place image at point, centering it
fig.figimage(im,pixelPoint[0]-image_size[0]/2,pixelPoint[1]-image_size[1]/2,origin="upper")
plt.show()
Produces:
Following on from Mark's answer. I just thought I would add to this a bit because I tried to run this and it does what I want with the exception of actually displaying the icons on the graph. Maybe something has changed with matplotlib. It has been 4 years.
The line of code that reads:
ax.get_frame().set_alpha(0)
does not seem to work, however
ax.patch.set_alpha(0)
does work.
The other answer may lead to problems when resizing the figure. Here is a different approach, positionning the images inside annotation boxes, which are anchored in data coordinates.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
path = "https://upload.wikimedia.org/wikipedia/commons/b/b5/Tango-example_icons.png"
image = plt.imread(path)[116:116+30, 236:236+30]
x = np.arange(10)
y = np.random.rand(10)
fig, ax = plt.subplots()
ax.plot(x,y)
def plot_images(x, y, image, ax=None):
ax = ax or plt.gca()
for xi, yi in zip(x,y):
im = OffsetImage(image, zoom=72/ax.figure.dpi)
im.image.axes = ax
ab = AnnotationBbox(im, (xi,yi), frameon=False, pad=0.0,)
ax.add_artist(ab)
plot_images(x, y, image, ax=ax)
plt.show()

Display a colorbar without associated image map

I have matplotlib figure that displays an image, histogram and a colobar under the histogram, generated by the following code:
import numpy as np
import imageio
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
image = imageio.imread('imageio:camera.png')
fig, axes = plt.subplots(2,1,figsize=(8,4))
divider = make_axes_locatable(axes[1])
cbar_ax = divider.append_axes("bottom", size='15%', pad=0.02)
im = axes[0].imshow(image,cmap='gray')
values, bins, patches = axes[1].hist(image.ravel(), 255, color='#cccccc', density=True)
cbar = plt.colorbar(im, cax=cbar_ax, orientation='horizontal')
fig.savefig('test.png', dpi=300)
plt.show()
This code generates exactly this image below
Since it's a recurrent image (I'm studying the histogram), I would like to create a figure just displaying the histogram and the associated colorbar below it.
But to display a colorbar is mandatory provides a image_map as argument and I need to call ax.imshow() to have a image_map. The exact output I want is something like
And I don't know how to achieve that.
Of course I could edit my images in some editor (as I did), but this isn't acceptable, since every update takes me a huge effort editing many image.

Matplotlib canvas as numpy array artefacts

I want to convert a matplotlib figure into a numpy array. I have been able to do this by accessing the contents of the renderer directly. However, when I call imshow on the numpy array it has what looks like aliasing artefacts along the edges which aren't present in the original figure.
I've tried playing around with various parameters but can't figure out how to fix the artefacts from imshow. The differences in the images remain if I save the figures to an image file.
Note that what I want to achieve is a way to confirm that the content of the array is the same as the figure I viewed before. I think probably these artefacts are not present in the numpy array but are created during the imshow call. Perhaps approriate configuration of imshow can resolve the problem.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
import math
fig = plt.figure(frameon=False)
ax = plt.gca()
ax.add_patch(Rectangle((0,0), 1, 1, angle=45, color="red"))
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_aspect(1)
plt.axis("off")
fig.canvas.draw()
plt.savefig("rec1.png")
plt.show()
X = np.array(fig.canvas.renderer._renderer)
fig = plt.figure(frameon=False)
ax = plt.gca()
plt.axis("off")
plt.imshow(X)
plt.savefig("rec2.png")
plt.show()
These are clearly resampling artefacts, which can be avoided by using plt.figimage which specifically adds a non-resampled image to the figure.
plt.figimage(X)
plt.show()
Note that this will not work with the %matplotlib inline in Jupyter Notebook, but it does work fine with %matplotlib notebook and with GUI backends.
By adding the fig.tight_layout with padding of -1.08, I was able to get the exact image as the real image.
X = np.array(fig.canvas.renderer._renderer)
fig = plt.figure(frameon=False)
ax = plt.gca()
plt.axis("off")
plt.imshow(X)
fig.tight_layout(pad=-1.08)
plt.savefig("rec2.png")
plt.show()
Real Image
From numpy array
I hope that solves your problem, atleast till you find a better way. Cheers.
The best one I can think of is by using cv2 (openCV-python) library. My solution does require saving the image and in the case of color images, the decoded images will have the channels stored in B G R order.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
import math
import cv2 #import openCV
fig = plt.figure(frameon=False)
ax = plt.gca()
ax.add_patch(Rectangle((0,0), 1, 1, angle=45, color="red"))
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_aspect(1)
plt.axis("off")
fig.canvas.draw()
plt.savefig("rec1.png")
plt.show()`
im = cv2.imread("rec1.png")
print(type(im)) #prints numpy.ndarray
cv2.imshow("pic",im) #creates a window named pic, loads im
cv2.waitKey(0) #has no time limit, window destroyed on any key press
cv2.destroyAllWindows()
Final result looks like
Since it is a numpy array, you can call methods on it for your comparison.
print(im.shape) #prints (288, 432, 3)
The image that is shown in the second plot is plotted smaller than the first image; the reason is that the complete first figure's image is squeezed into a newly created smaller axes -- this would be obvious when not turning the axes off.
In order to make sure the second figure only shows the image itself, you may adjust the margins, such that there is no spacing between the figure edge and the axes, using subplots_adjust.
fig = plt.figure(frameon=False)
fig.subplots_adjust(0,0,1,1)
ax = plt.gca()
plt.axis("off")
plt.imshow(X)
This produces the desired plot.
Note however that the array is not exactly the same due to antialiasing being applied when saving the png file. You may find out via
X = np.array(fig.canvas.renderer._renderer)/255.
Y = plt.imread("rec1.png")
print(np.all(X==Y))
## This prints False
Inversely speaking, if you want to have the same numpy array as the one that is saved, you should make sure to use the saved image itself.
plt.savefig("rec1.png")
X = plt.imread("rec1.png")
# use X from here onwards
Thanks to the comments who pointed out interpolation as the cause. I found the following code (adapted for Python 3) which displays the image in the way I want; identical to the first image but via the numpy array.
import PIL.Image
from io import BytesIO
import IPython.display
import numpy as np
def showarray(a, fmt='png'):
a = np.uint8(a)
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
IPython.display.display(IPython.display.Image(data=f.getvalue()))
source: https://gist.github.com/kylemcdonald/2f1b9a255993bf9b2629

Save figure with clip box from another figure

Normally if you plot two different figures using the default settings in pyplot, they will be exactly the same size, and if saved can be neatly aligned in PowerPoint or the like. I'd like to generate one figure, however, which has a legend outside of the figure. The script I'm using is shown below.
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1,201)
y1=x**2
y2=np.sin(x)
fig1=plt.figure(1)
plt.plot(x,y1,label='y1')
handles1,labels1=plt.gca().get_legend_handles_labels()
lgd1=plt.gca().legend(handles1,labels1,bbox_to_anchor=(1.27,1),borderaxespad=0.)
fig2=plt.figure(2)
plt.plot(x,y2)
fig1.savefig('fig1',bbox_extra_artists=(lgd1,),bbox_inches='tight')
fig2.savefig('fig2')
plt.show()
The problem is that in PowerPoint, I can no longer align the two figures left and have their axes aligned. Due to the use of the 'extra artists' and 'bbox_inches=tight' arguments for the first figure, the width of its margins becomes different from the second figure.
Is there any way to 'transfer' the clip box from the first figure to the second figure, such that they can be aligned by 'align left' in PowerPoint?
I think an easier way to achieve what you want is to just construct one figure with two subplots, and let matplotlib align everything for you.
Do you think doing something like this is a good idea?
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,1,201)
y1=x**2
y2=np.sin(x)
fig = plt.figure()
a = fig.add_subplot(211)
a.plot(x,y1, label='y1')
lgd1 = a.legend(bbox_to_anchor = (1.27,1), borderaxespad=0.)
a = fig.add_subplot(212)
a.plot(x,y2)
fig.savefig('fig',bbox_extra_artists=(lgd1,),bbox_inches='tight')

Categories