Moving x or y ticks in Matplotlib up - matplotlib

I have a numpy array with random values. I have plotted the values in the array using imshow() so that each element shows as a grey-scale square. The problem is that the labels (0, 1, 2 etc) start at the bottom corner. I would like to move them along a bit so they are centred underneath each square. Is there a straight-forward way of doing this?

Just found http://matplotlib.sourceforge.net/examples/pylab_examples/image_interp.html
and the most straightforward way is just to have grid(True) ->woohoo!

Related

python numpy/scipy zoom changing center

I have a 2D numpy array, say something like:
import numpy as np
x = np.random.rand(100, 100)
Now, I want to keep zoom this image (keeping the size the same i.e. (100, 100)) and I want to change the centre of the zoom.
So, say I want to zoom keeping the point (70, 70) at the centre and normally how one would do it is to "translate" the image to that point and then zoom.
I wonder how I can achieve this with scipy. I wonder if there is way to specify say 4 coordinates from this numpy array and basically fill the canvas with the interpolated image from this region of interest?
You could use ndimage.zoom to do the zooming part. I use ndimage a lot, and it works well and is fast. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html
The 4 coordinates part you mention is I presume two corners of region you want to zoom into. That's easy by just using numpy slicing of your image (presuming your image is an np array):
your_image[r1:r2, c1:c2]
Assuming you want your output image at 100x100, then your r1-r2, and c1-c2 differences will be the same, so your region is square.
nd.zoom takes a zoom factor (float). You would need to compute whta athat zoom factor is in order to take your sliced image and turn it into a 100x100 sized array:
ndimage.zoom(your_image[r1:r2, c1:c2], zoom=your_zoom_factor)

Using matplotlib to plot a matrix with the third variable as source for a color map

Say you have the matrix given by three arrays, being:
x = N-dimensional array.
y = M-dimensional array.
And z is a set of "somewhat random" values from -0.3 to 0.3 in a NxM shape. I need to create a plot in which the x values are in the x-axis, y values are in the y-axis and using z as the source to indicate the intensity of each pixel with a color map.
So far, I have tried using
plt.contourf(x,y,z)
and the resulting plot is very nice for me (attached at the end of this paragraph), but a smoothing is automatically applied to the plot! I need to be able to distinguish the pixels and I cannot find a way to do it.
contourf result
I have also studied the possibility of using
ax.matshow(z)
in order to sucesfully see the pixels... but then I am struggling trying to personalize the x and y axis, since only the index of the pixel is shown (see below).
matshow result
Would you please give me some ideas? Thank you.
Without more information on your x,y data it's hard to know, but I would guess you are looking for pcolormesh.
plt.pcolormesh(x,y,z)
This would take the x and y data as input and hence shows the z data at the appropriate coordinates.
You can use imshow with the keyword interpolation='nearest'.
plt.imshow(z, interpolation='nearest')

Matplotlib tick labels

Is there a way to render the tick labels just right inside the axes, i.e, something like the direction property there is on the ticks themself?
Right now I'm setting the x property to a positive value on the ticklabels to draw them inside of the axis, i.e.,
ax2.set_yticklabels(['0', '2500', '5000', '7500'], minor=False, x=0.05)
But this doesn't really work on resizable plots, as the 0.05 figure is absolute (and too big on big plots).
Any ideas?
I'm assuming that ax2 is constructed as ax2 = ax.twinx(), which is to say that it is on the right side of the axes.
You could do something like the following:
ax2.set_yticklabels(['0', '2500', '5000', '7500'], minor=False, horizontalalignment='right')
for tick in ax2.yaxis.get_major_ticks():
tick.set_pad(-8)
If you want the left side axis on the inside too, then you'd simply switch the horizontal alignment to 'left' and change the pad from -8 to -25.
The two numbers might not be exact and could depend on other matplotlib settings you might have (e.g. length of major ticks) so you may want to increase or decrease those values slightly.

Change y_axis to begin 0 on top

By default, when I add axes to an image in matplotlib, the x axis begins at 0 and increases from left to right and the y-axis begins at 0, increasing from bottom to top. I would like to have the y-axis beginning at zero, but from top to bottom (that is, 0 on the top, and the maximum value on the bottom) How could I accomplish this?
If I understand correctly, you're asking how to reverse the y-axis. This can be done with
plt.gca().invert_yaxis()
which takes the current axis plt.gca() and calls its method invert_yaxis() to invert the y-axis.
You can also simply call plt.ylim() and put the coordinates in reverse order. I know I always fine-tune the range of all plots by hand anyway, so this is easier in that situation. So let's say you have a plot that runs from 0 to 10, you would just call
plt.ylim(10,0)
and it will flip the y-axis.

Positioning figure and table in figure

I'm trying to put both a plot and a table in a figure, but I want some whitespace to separate the two. How do I position the table/plot at arbitrary positions? What I have now is the table of values showing up IMMEDIATELY under the x-axis (so that it's actually colliding into my axis labels...)
I don't know matplotlib at all...The documentation is not written very well either...
To position something in a figure you have to use the function set_position([left, bottom, width, height]). Example:
matplotlib.pyplot.axes().set_position([0.15, 0.20, 0.80, 0.70])