last day (16 days later) » 

9:02 PM
0
Q: Labeling axes with non uniform ticks in pyplot

Riptyde4I have a graph that is plotted against two axes. The x-axis is increasing, while the y-axis may decrease and then increase again, or it may increase the whole time, or it may decrease the whole time. It all depends on the input to the program. This question ONLY concerns the non-uniform y-axis...

 
Provide minimal example please
 
@MadPhysicist Thanks for the response. Edited question, let me know if you need more
 
Just to be clear, you want to change the labels, not the values of the y-axis that they correspond to, right?
Nevermind, just realized you are trying to do set_ticks as well.
 
This is for an image plot with a colorbar, so the y-axis values are estimated via the range of the values anyways - so, yes, just the labels
 
Would you be OK with having a monotonically increasing axis, but with labels that were not monotonically increasing?
If just the labels, the easiest way is probably fig5ax.yaxis.set_ticklabels([str(x) for x in rangeLabelSet]). The values won't be messed with if they are strings.
 
9:02 PM
@MadPhysicist I don't honestly know what I should be doing. (if I even need set_ticks) .. I just need the labels that I specify to be evenly spaced across the Y-axis, in the order I specify. This is for an image plot with a colorbar, so the y-axis values are estimated via the range of the values anyways - so, yes, just the labels
@MadPhysicist Is that different then fig5ax.yaxis.set_ticklabels(rangeLabelSet) ?? I've tried this and It refuses to line up well. I think I'm using the wrong locator or something
 
If your tick labels are [1,10,30,40,50,60,40,30,20,1], what are the actual y-values? Are they just integers up to the size of the image?
 
@MadPhysicist The graph has an x and y axis, where the value in each cell [x,y] in the image plot is a function of x and y and is labeled by a color. The pyplot.implot just plots the matrix and I'm trying to label the axes according to the progression of Y along the matrix. The values of x and y and their progressions are dynamic via user input.
@MadPhysicist Those are the actual y-values.
hey
thanks for the help amigo
 
So let me see if I understand your problem. You have a vector of x-values a and a vector of y-values. You then have a matrix in which every cell [i, j] is computed from x[i], y[j]?
 
yep
you got it
 
When you plot with pyplot, it just labels it from 0 to N, where N=len(y)?
I mean before you do any modifications.
 
9:05 PM
yes
If I give it the extent, which is minY to maxY it will just label it in increasing order from the
Y = 0 to Y = maxRange
but I need it to label it with the values that I specify, in the same order I specify - I have accomplished this with set_ticklabels, but for some reason my ticklabels are being cut off of the graph in different ways every time, it's far from consistent
 
OK. So the confusion you are having is between what you call "Y" and the y-axis on the chart. You want to replace the index j that pyplot displays with y[j] from your y-axis
sorry for going so slow. I just want to make sure I understand properly before I give you any advice
 
yes
understood, no problem, thanks for taking your time
exactly
 
OK. So here's a thing about pyplot. The y-values it sees internally will not change. You have to set a formatter, which is a map from the index to the label you want to see. set_tick_labels should be setting the formatter correctly, but for some reason is not. Try this: fig5ax.yaxis.set_major_formatter(mpl.ticker.FixedLocator([str(x) for x in rangeLabelSet]))
I am not sure you need the str(x) comprehension, but start with that to be safe.
 
Ok. Let me plug it in quick
 
You will also need to import matplotlib as mpl
Another point is that yloc = pyplot.MaxNLocator(nbins=len(rangeLabelSet), prune=None, trim=False, symmetric=False) A) does not actually set the locator, and B) Would format the image axis as something unexpected if it worked.
 
9:14 PM
Getting an error "AttributeError: FixedLocator instance has no attribute 'set_locs'
"
 
Oops. I meant FixedFormatter instead of FixedLocator.
 
yes I have a call to set major locator below that, it was the only way I could get it to stop arranging my ticklabels for me
ah ok. it runs now
but my labels are in increasing order from bottom to top again
 
You should not need to do anything with the locator. When you display an image, the locator is set correctly for you.
oh?
can you post a pic in your question?
 
        fig5ax.yaxis.set_ticks(rangeLabelSet)
        fig5ax.yaxis.set_major_formatter(plt.ticker.FixedFormatter([str(x) for x in rangeLabelSet]))
it's the only code I'm running right now
 
fig5ax.yaxis.set_ticks(rangeLabelSet) messes up the locator. You should remove it.
 
9:18 PM
I can't actually it's kind of a hassle, but just imagine a graph with 30 at the bottom of the y axis, scaling up to 510, where in reality (via the ordering in the array rangeLabelSet), 30 should be towards the middle
ah.
ok that fixed the ordering
 
but now a bunch of my labels are cut off
 
OK. I'm going to explain a bit before I tell you how to fix it.
 
ok thanks
 
When you do fig5ax.yaxis.set_ticks(rangeLabelSet), it sets the locations of the visible ticks to the y-vector that you have. However, your image is actually displayed in the range 0-len(rangeLabelSet) on the graph's y-axis.
So whenever you want to mess with the locator or formatter, you have to remember that pyplot is mapping all your labels to 0-9
 
9:22 PM
got ya
 
The formatter is basically a map from input graph axis value to string label. You have that one correct. Now you just need to adjust your axis to show the max value of the image. Something like fig5ax.set_ylim(0, len(rangeLabelSet)) should do it, although you may need to add a margin of 0.5 to take pixel size into account: fig5ax.set_ylim(-0.5, len(rangeLabelSet) + 0.5)
 
Ok let me try that, thanks.
Is it always the number of labels for the ylim? Or should it be the number of rows in my matrix
 
number of rows
i thought they were the same
because if not, that's a whole 'nother can of worms
 
sorry if I failed to explain that. There are 80 rows, and about 400 columns... I chose 9 labels as a good number of labels to display
oh no lol
 
No worries. That would be where you would need a locator.
Is there a variable that contains the row numbers corresponding to the labels?
 
9:30 PM
yes
 
what is it called? that way I can use that name and you can copy and paste
 
its called the array of row values is called diffRanges so that number would be len(diffRanges)
I called set_ylim(0, len(diffRanges)) and my plot just went away completely. It seems set_ylim(min(diffRanges), max(diffRanges)) does the trick
 
You don't need to do anything to ylim most likely. It may be better to do fig5ax.yaxis.set_major_locator(plt.ticker.FixedLocator(diffRanges))
That will ensure that you get the correct rows even if they are not evenly spaced or something like that.
 
Looks like it threw in a couple of labels already without even having to set them
I'd still like to have my label set though
(or more labels)
 
In the line fig5ax.yaxis.set_major_locator(plt.ticker.FixedLocator(diffRanges)), I meant that diffRanges should be only the indices corresponding to the rows that are in rangeLabelSet, not the whole y-axis
A screenshot and the exact code would have made this much easier by the way.
Also, I just noticed that you are using extent, at least in your comment. Could you please post the code you use to set up the figure?
 
9:42 PM
MyExtent = [minFrequency, maxFrequency, minRange, maxRange]
fig5 = pyplot.figure(5, figsize=(10,9), facecolor='white')
pyplot.gcf().clear()
plt.rcParams.update({'font.size': 18})
fig5ax = pyplot.axes()
            implot = pyplot.imshow(valgrid, extent=MyExtent , aspect='auto', vmin = myVmin, vmax = myVmax)
oo.. just writing out that list of inces that correspond to rows now
it's tough because some rows may have corresponding Y values that are very close to the others, since the graph can do this [10.123, 10.23123, 15.1248, 10.22]
but I'm starting to see where to go with this, you've been very helpful
**not writing out, but constructing a dictionary
 

  last day (16 days later) »