2

I have my image enter image description here

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?

Richard Rublev
  • 7,718
  • 16
  • 77
  • 121

4 Answers4

2

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.

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
0

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

Community
  • 1
  • 1
pltrdy
  • 2,069
  • 1
  • 11
  • 29
0

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

BChow
  • 471
  • 2
  • 7
0

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.

Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • @ Works perfect,but how to get rid of 0,100,200,300 ticks I do not need them at all? – Richard Rublev Feb 24 '16 at 16:25
  • @ kwinkunks Works perfect but how to get rid of 0,100,200 ticks? – Richard Rublev Feb 24 '16 at 16:26
  • There are lots of ways to do most things in mpl! `ax.set_axis_off()` will turn off the whole border, for example, or `ax.set_xticks([]); ax.set_yticks([])` will remove the ticks only. You can also use [`plt.tick_params`](http://stackoverflow.com/a/12998531/3381305). – Matt Hall Feb 24 '16 at 16:38
  • Note that `imshow` can be an `Axes` method if you want it to be: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.imshow – tmdavison Feb 24 '16 at 16:44
  • tom - Thanks! Good point, I corrected my post. In think I use `plt.imshow` because then `plot.colorbar()` 'just works'... and I've never taken the trouble to figure out the way to do this with `ax`. – Matt Hall Feb 24 '16 at 17:27