Discussion:
[Matplotlib-users] MultipleLocator and MaxNLocator
Tommy Carstensen
2015-02-14 01:29:02 UTC
Permalink
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
Eric Firing
2015-02-14 02:01:46 UTC
Permalink
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.

Eric
Tommy Carstensen
2015-02-14 15:47:42 UTC
Permalink
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Eric Firing
2015-02-14 17:17:11 UTC
Permalink
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
What is the effect you are trying to achieve? How would you describe
the desired tick placement algorithm?

Eric
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Ryan Nelson
2015-02-14 17:27:32 UTC
Permalink
Tommy, (Sorry for the doubleup. I just realized I forgot to hit reply-all.)

Do you want to remove the tick at 0 and only have 5,10, etc.? Could you
just do something like this instead:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()

Ryan


On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen <
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Post by Eric Firing
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
Post by Eric Firing
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take
a
Post by Eric Firing
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 17:33:14 UTC
Permalink
This post might be inappropriate. Click to display it.
Eric Firing
2015-02-14 18:19:06 UTC
Permalink
Post by Tommy Carstensen
Thanks again Ryan. That's exactly what I want to achieve; i.e. remove
the tick at 0 and only keep 5 and 10. Your solution works, but it's a
bit of hack to use magic constants. I could however get those values
from the xlim.
Eric, I would describe the desired tick placement algorithm as
ax1.xaxis.set_major_locator(MaxNLocator(prune='lower'))
Aha! The problem is that the MaxNLocator is the only one with the prune
kwarg. It could be added to the MultipleLocator. For now, though, you
can make your own specialized Locator, hardwired to omit the first tick,
like this:

from matplotlib.ticker import MultipleLocator

class TrimmedMultipleLocator(MultipleLocator):
def tick_values(self, vmin, vmax):
return MultipleLocator.tick_values(self, vmin, vmax)[1:]

then just use

ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))

I haven't tested it--but give it a try. What it is doing is making a
subclass of MultipleLocator, and altering only the one little bit of its
behavior that you want to modify. Everything else is automatically
inherited from the base class, MultipleLocator.

Eric
Post by Tommy Carstensen
ax1.xaxis.set_major_locator(MultipleLocator(5))
Post by Ryan Nelson
Tommy, (Sorry for the doubleup. I just realized I forgot to hit reply-all.)
Do you want to remove the tick at 0 and only have 5,10, etc.? Could you just
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()
Ryan
On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 18:45:47 UTC
Permalink
Erik, that doesn't seem to work either. I tried this:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
class TrimmedMultipleLocator(MultipleLocator):
def tick_values(self, vmin, vmax):
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()

Here is an example of the use of prune='lower', but it does not allow
one to set the tick step size:
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib

I think my best bet is to just set those ticks manually.
Post by Eric Firing
Post by Tommy Carstensen
Thanks again Ryan. That's exactly what I want to achieve; i.e. remove
the tick at 0 and only keep 5 and 10. Your solution works, but it's a
bit of hack to use magic constants. I could however get those values
from the xlim.
Eric, I would describe the desired tick placement algorithm as
ax1.xaxis.set_major_locator(MaxNLocator(prune='lower'))
Aha! The problem is that the MaxNLocator is the only one with the prune
kwarg. It could be added to the MultipleLocator. For now, though, you
can make your own specialized Locator, hardwired to omit the first tick,
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
then just use
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
I haven't tested it--but give it a try. What it is doing is making a
subclass of MultipleLocator, and altering only the one little bit of its
behavior that you want to modify. Everything else is automatically
inherited from the base class, MultipleLocator.
Eric
Post by Tommy Carstensen
ax1.xaxis.set_major_locator(MultipleLocator(5))
Post by Ryan Nelson
Tommy, (Sorry for the doubleup. I just realized I forgot to hit reply-all.)
Do you want to remove the tick at 0 and only have 5,10, etc.? Could you just
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()
Ryan
On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Ryan Nelson
2015-02-14 19:01:45 UTC
Permalink
Tommy,

I'm sorry. I forgot to hit send all *again*. Below is my original message,
but the function I wrote is updated because it wasn't exactly correct....

Ah. I was working on something to help out, so I'm just seeing Eric's very
elegant solution, which I have yet to try. However, I feel like you might
run into some problems if you always drop the first tick. For example, try
this plot:
______________
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(2,12,1000)
ys = np.sin(xs)
n = 5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
ax1.xaxis.set_major_locator(MultipleLocator(5))
plt.show()
_____________
In this case, dropping the first tick will result in only one tick on the
screen.

What is your use-case? Are you annoyed that the axis labels are overlapping
at the far left? If that's the case, here's a little function (trimticks)
that I whipped up that might help. It drops the far left or far right label
if it is exactly at the edge of the axes. Should work for y axes as well.
_____________
def trimticks(ax, n=5):
xmin, xmax = ax.get_xlim()
if xmin%n == 0:
xmin = xmin+n
else:
xmin = xmin + n - xmin%n

if not xmax%n == 0:
xmax = xmax + n - xmax%n

ticks = np.arange(xmin, xmax, n)
ax.set_xticks(ticks)

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(0,20,10000)
ys = np.sin(xs)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
trimticks(ax1)
plt.show()

___________________


On Sat, Feb 14, 2015 at 1:45 PM, Tommy Carstensen <
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()
Here is an example of the use of prune='lower', but it does not allow
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib
I think my best bet is to just set those ticks manually.
Post by Eric Firing
Post by Tommy Carstensen
Thanks again Ryan. That's exactly what I want to achieve; i.e. remove
the tick at 0 and only keep 5 and 10. Your solution works, but it's a
bit of hack to use magic constants. I could however get those values
from the xlim.
Eric, I would describe the desired tick placement algorithm as
ax1.xaxis.set_major_locator(MaxNLocator(prune='lower'))
Aha! The problem is that the MaxNLocator is the only one with the prune
kwarg. It could be added to the MultipleLocator. For now, though, you
can make your own specialized Locator, hardwired to omit the first tick,
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
then just use
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
I haven't tested it--but give it a try. What it is doing is making a
subclass of MultipleLocator, and altering only the one little bit of its
behavior that you want to modify. Everything else is automatically
inherited from the base class, MultipleLocator.
Eric
Post by Tommy Carstensen
ax1.xaxis.set_major_locator(MultipleLocator(5))
Post by Ryan Nelson
Tommy, (Sorry for the doubleup. I just realized I forgot to hit
reply-all.)
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Do you want to remove the tick at 0 and only have 5,10, etc.? Could
you just
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()
Ryan
On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you
know
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
a
look and join the conversation now.
http://goparallel.sourceforge.net/
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take a
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
look and join the conversation now.
http://goparallel.sourceforge.net/
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Eric Firing
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
Post by Eric Firing
Post by Tommy Carstensen
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take a
Post by Eric Firing
Post by Tommy Carstensen
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Eric Firing
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
Post by Eric Firing
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take
a
Post by Eric Firing
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 19:18:05 UTC
Permalink
Ryan, my use case is indeed that I want to avoid overlapping ticks and
I want to avoid them by not displaying them. Here is a guy with the
same problem:
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib

Here is the problem at the top left of my plot:
www.tommycarstensen.com/matplotlib.png

I'll just set the ticks manually. Sadly seems like the easiest thing to do.
Post by Virgil Stokes
Tommy,
I'm sorry. I forgot to hit send all *again*. Below is my original message,
but the function I wrote is updated because it wasn't exactly correct....
Ah. I was working on something to help out, so I'm just seeing Eric's very
elegant solution, which I have yet to try. However, I feel like you might
run into some problems if you always drop the first tick. For example, try
______________
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(2,12,1000)
ys = np.sin(xs)
n = 5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
ax1.xaxis.set_major_locator(MultipleLocator(5))
plt.show()
_____________
In this case, dropping the first tick will result in only one tick on the
screen.
What is your use-case? Are you annoyed that the axis labels are overlapping
at the far left? If that's the case, here's a little function (trimticks)
that I whipped up that might help. It drops the far left or far right label
if it is exactly at the edge of the axes. Should work for y axes as well.
_____________
xmin, xmax = ax.get_xlim()
xmin = xmin+n
xmin = xmin + n - xmin%n
xmax = xmax + n - xmax%n
ticks = np.arange(xmin, xmax, n)
ax.set_xticks(ticks)
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(0,20,10000)
ys = np.sin(xs)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
trimticks(ax1)
plt.show()
___________________
On Sat, Feb 14, 2015 at 1:45 PM, Tommy Carstensen
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()
Here is an example of the use of prune='lower', but it does not allow
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib
I think my best bet is to just set those ticks manually.
Post by Eric Firing
Post by Tommy Carstensen
Thanks again Ryan. That's exactly what I want to achieve; i.e. remove
the tick at 0 and only keep 5 and 10. Your solution works, but it's a
bit of hack to use magic constants. I could however get those values
from the xlim.
Eric, I would describe the desired tick placement algorithm as
ax1.xaxis.set_major_locator(MaxNLocator(prune='lower'))
Aha! The problem is that the MaxNLocator is the only one with the prune
kwarg. It could be added to the MultipleLocator. For now, though, you
can make your own specialized Locator, hardwired to omit the first tick,
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
then just use
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
I haven't tested it--but give it a try. What it is doing is making a
subclass of MultipleLocator, and altering only the one little bit of its
behavior that you want to modify. Everything else is automatically
inherited from the base class, MultipleLocator.
Eric
Post by Tommy Carstensen
ax1.xaxis.set_major_locator(MultipleLocator(5))
Post by Ryan Nelson
Tommy, (Sorry for the doubleup. I just realized I forgot to hit reply-all.)
Do you want to remove the tick at 0 and only have 5,10, etc.? Could you just
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()
Ryan
On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take
a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Ryan Nelson
2015-02-14 19:42:25 UTC
Permalink
Yep. I see your problem. My function and Eric's object should help here.

A sore-spot with many folks coming over to Matplotlib from "X" is the fact
that MPL does not calculate the size of text until the plot is generated.
That means it doesn't always get text positioning, etc. exactly correct.
That takes a little getting used to, and for me, it is minor.

Admit it, Gnuplot as it's quirks as well :) I always hated that it
wouldn't cut off some markers at the edge of the screen. For example, with
Gnuplot 4.6rev5 the following
plot x with points ps 7
Leads to a bunch of markers running over the axes limits. (Maybe there is a
way to fix this now. Many years ago that was not the case.)

Ryan

On Sat, Feb 14, 2015 at 2:18 PM, Tommy Carstensen <
Post by Tommy Carstensen
Ryan, my use case is indeed that I want to avoid overlapping ticks and
I want to avoid them by not displaying them. Here is a guy with the
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib
www.tommycarstensen.com/matplotlib.png
I'll just set the ticks manually. Sadly seems like the easiest thing to do.
Post by Virgil Stokes
Tommy,
I'm sorry. I forgot to hit send all *again*. Below is my original
message,
Post by Virgil Stokes
but the function I wrote is updated because it wasn't exactly correct....
Ah. I was working on something to help out, so I'm just seeing Eric's
very
Post by Virgil Stokes
elegant solution, which I have yet to try. However, I feel like you might
run into some problems if you always drop the first tick. For example,
try
Post by Virgil Stokes
______________
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(2,12,1000)
ys = np.sin(xs)
n = 5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
ax1.xaxis.set_major_locator(MultipleLocator(5))
plt.show()
_____________
In this case, dropping the first tick will result in only one tick on the
screen.
What is your use-case? Are you annoyed that the axis labels are
overlapping
Post by Virgil Stokes
at the far left? If that's the case, here's a little function (trimticks)
that I whipped up that might help. It drops the far left or far right
label
Post by Virgil Stokes
if it is exactly at the edge of the axes. Should work for y axes as well.
_____________
xmin, xmax = ax.get_xlim()
xmin = xmin+n
xmin = xmin + n - xmin%n
xmax = xmax + n - xmax%n
ticks = np.arange(xmin, xmax, n)
ax.set_xticks(ticks)
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(0,20,10000)
ys = np.sin(xs)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
trimticks(ax1)
plt.show()
___________________
On Sat, Feb 14, 2015 at 1:45 PM, Tommy Carstensen
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()
Here is an example of the use of prune='lower', but it does not allow
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib
Post by Virgil Stokes
Post by Tommy Carstensen
I think my best bet is to just set those ticks manually.
Post by Eric Firing
Post by Tommy Carstensen
Thanks again Ryan. That's exactly what I want to achieve; i.e. remove
the tick at 0 and only keep 5 and 10. Your solution works, but it's a
bit of hack to use magic constants. I could however get those values
from the xlim.
Eric, I would describe the desired tick placement algorithm as
ax1.xaxis.set_major_locator(MaxNLocator(prune='lower'))
Aha! The problem is that the MaxNLocator is the only one with the
prune
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
kwarg. It could be added to the MultipleLocator. For now, though,
you
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
can make your own specialized Locator, hardwired to omit the first
tick,
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
then just use
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
I haven't tested it--but give it a try. What it is doing is making a
subclass of MultipleLocator, and altering only the one little bit of
its
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
behavior that you want to modify. Everything else is automatically
inherited from the base class, MultipleLocator.
Eric
Post by Tommy Carstensen
ax1.xaxis.set_major_locator(MultipleLocator(5))
Post by Ryan Nelson
Tommy, (Sorry for the doubleup. I just realized I forgot to hit reply-all.)
Do you want to remove the tick at 0 and only have 5,10, etc.? Could you just
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()
Ryan
On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying
out
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when
you
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
know
what you want your tick interval to be; MaxNLocator is for when
you
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot
Media,
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and
more.
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Eric Firing
Take
a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel
Website,
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
sponsored by Intel and developed in partnership with Slashdot
Media,
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
is
your
hub for all things parallel software development, from weekly
thought
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Post by Ryan Nelson
Post by Tommy Carstensen
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now.
http://goparallel.sourceforge.net/
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Post by Tommy Carstensen
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
a
look and join the conversation now.
http://goparallel.sourceforge.net/
Post by Virgil Stokes
Post by Tommy Carstensen
Post by Eric Firing
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Virgil Stokes
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take a
Post by Virgil Stokes
Post by Tommy Carstensen
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Virgil Stokes
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
Post by Virgil Stokes
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take
a
Post by Virgil Stokes
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 19:47:38 UTC
Permalink
Ryan, I stopped using gnuplot, because it requires the data to be
formatted in very specific ways :) I remember having functions just to
format the input data correctly for heat plots and the wrapper scripts
I wrote were quite convoluted. Matplotlib has its advantages for sure.
Otherwise I would not have switched. I'm just frustrated with being
back at the start line. Thanks for your help and bearing with my
impatience.
Post by Ryan Nelson
Yep. I see your problem. My function and Eric's object should help here.
A sore-spot with many folks coming over to Matplotlib from "X" is the fact
that MPL does not calculate the size of text until the plot is generated.
That means it doesn't always get text positioning, etc. exactly correct.
That takes a little getting used to, and for me, it is minor.
Admit it, Gnuplot as it's quirks as well :) I always hated that it wouldn't
cut off some markers at the edge of the screen. For example, with Gnuplot
4.6rev5 the following
plot x with points ps 7
Leads to a bunch of markers running over the axes limits. (Maybe there is a
way to fix this now. Many years ago that was not the case.)
Ryan
On Sat, Feb 14, 2015 at 2:18 PM, Tommy Carstensen
Post by Tommy Carstensen
Ryan, my use case is indeed that I want to avoid overlapping ticks and
I want to avoid them by not displaying them. Here is a guy with the
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib
www.tommycarstensen.com/matplotlib.png
I'll just set the ticks manually. Sadly seems like the easiest thing to do.
Post by Virgil Stokes
Tommy,
I'm sorry. I forgot to hit send all *again*. Below is my original message,
but the function I wrote is updated because it wasn't exactly correct....
Ah. I was working on something to help out, so I'm just seeing Eric's very
elegant solution, which I have yet to try. However, I feel like you might
run into some problems if you always drop the first tick. For example, try
______________
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(2,12,1000)
ys = np.sin(xs)
n = 5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
ax1.xaxis.set_major_locator(MultipleLocator(5))
plt.show()
_____________
In this case, dropping the first tick will result in only one tick on the
screen.
What is your use-case? Are you annoyed that the axis labels are overlapping
at the far left? If that's the case, here's a little function (trimticks)
that I whipped up that might help. It drops the far left or far right label
if it is exactly at the edge of the axes. Should work for y axes as well.
_____________
xmin, xmax = ax.get_xlim()
xmin = xmin+n
xmin = xmin + n - xmin%n
xmax = xmax + n - xmax%n
ticks = np.arange(xmin, xmax, n)
ax.set_xticks(ticks)
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
xs = np.linspace(0,20,10000)
ys = np.sin(xs)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(xs, ys)
trimticks(ax1)
plt.show()
___________________
On Sat, Feb 14, 2015 at 1:45 PM, Tommy Carstensen
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()
Here is an example of the use of prune='lower', but it does not allow
http://stackoverflow.com/questions/9422587/overlapping-y-axis-tick-label-and-x-axis-tick-label-in-matplotlib
I think my best bet is to just set those ticks manually.
Post by Eric Firing
Post by Tommy Carstensen
Thanks again Ryan. That's exactly what I want to achieve; i.e. remove
the tick at 0 and only keep 5 and 10. Your solution works, but it's a
bit of hack to use magic constants. I could however get those values
from the xlim.
Eric, I would describe the desired tick placement algorithm as
ax1.xaxis.set_major_locator(MaxNLocator(prune='lower'))
Aha! The problem is that the MaxNLocator is the only one with the prune
kwarg. It could be added to the MultipleLocator. For now, though, you
can make your own specialized Locator, hardwired to omit the first tick,
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
then just use
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
I haven't tested it--but give it a try. What it is doing is making a
subclass of MultipleLocator, and altering only the one little bit of its
behavior that you want to modify. Everything else is automatically
inherited from the base class, MultipleLocator.
Eric
Post by Tommy Carstensen
ax1.xaxis.set_major_locator(MultipleLocator(5))
Post by Ryan Nelson
Tommy, (Sorry for the doubleup. I just realized I forgot to hit reply-all.)
Do you want to remove the tick at 0 and only have 5,10, etc.? Could
you just
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(range(5,11,5))
ax1.plot(range(11))
plt.show()
Ryan
On Sat, Feb 14, 2015 at 10:47 AM, Tommy Carstensen
Post by Tommy Carstensen
Thanks for you answer Eric. I had to get some sleep before trying out
things. I currently have the code below, but it does not remove the
zero value tick. It removes the tick at 5 and 10 however.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(MultipleLocator(5))
xticks = ax1.xaxis.get_major_ticks()
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
ax1.set_xticks(ax1.get_xticks()[1:-1])
ax1.plot(list(range(11)))
plt.show()
Post by Eric Firing
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
They are for different situations. MultipleLocator is for when you
know
what you want your tick interval to be; MaxNLocator is for when you
don't know that, but you do know roughly how many ticks you want, and
what sort of numerical intervals are acceptable.
Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take
a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more.
Take a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take
a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Eric Firing
2015-02-14 19:09:30 UTC
Permalink
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()
Are you sure? I tried it, and it works for me. See attached script and
output.

Eric
Tommy Carstensen
2015-02-14 19:15:01 UTC
Permalink
Eric, it works if I do:
return MultipleLocator.tick_values(self, vmin, vmax)[2:]

But not if I do as first suggested by you:
return MultipleLocator.tick_values(self, vmin, vmax)[1:]

I don't understand this behaviour. It should be [1:]. I'll just set
the ticks manually. Seems to be the easiest thing. It would be
awesome, if MPL had the same behaviour as gnuplot, which allows me to
simply do:
set xtics <start>, <incr>
Post by Eric Firing
Post by Tommy Carstensen
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.xaxis.set_major_locator(TrimmedMultipleLocator(5))
#xticks[0].label1.set_visible(False)
#xticks[-1].label1.set_visible(False)
#ax1.set_xticks(ax1.xaxis.get_major_ticks()[1:-1])
ax1.plot(list(range(21)))
plt.show()
Are you sure? I tried it, and it works for me. See attached script and
output.
Eric
Eric Firing
2015-02-14 19:37:11 UTC
Permalink
Post by Tommy Carstensen
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
Are you using my test script but getting a different result? If not,
what is the difference in your test script?
Post by Tommy Carstensen
I don't understand this behaviour. It should be [1:]. I'll just set
the ticks manually. Seems to be the easiest thing. It would be
awesome, if MPL had the same behaviour as gnuplot, which allows me to
set xtics <start>, <incr>
def xtics(ax, start, incr):
stop = ax.dataLim.x1 + 0.01 * incr
ax.xaxis.set_ticks(np.arange(start, stop, incr))


Now invoke that function *after* all your plot calls, so that the
dataLim bounding box includes all the data in your plot.

Eric
Tommy Carstensen
2015-02-14 19:39:16 UTC
Permalink
Thanks Eric. I decided to get peace of mind and just set the tick
labels manually. I can't afford to spend several hours on all of my
plots. I appreciate your help a lot.
Post by Tommy Carstensen
return MultipleLocator.tick_values(self, vmin, vmax)[2:]
return MultipleLocator.tick_values(self, vmin, vmax)[1:]
Are you using my test script but getting a different result? If not, what
is the difference in your test script?
Post by Tommy Carstensen
I don't understand this behaviour. It should be [1:]. I'll just set
the ticks manually. Seems to be the easiest thing. It would be
awesome, if MPL had the same behaviour as gnuplot, which allows me to
set xtics <start>, <incr>
stop = ax.dataLim.x1 + 0.01 * incr
ax.xaxis.set_ticks(np.arange(start, stop, incr))
Now invoke that function *after* all your plot calls, so that the dataLim
bounding box includes all the data in your plot.
Eric
Virgil Stokes
2015-02-14 10:33:11 UTC
Permalink
Post by Tommy Carstensen
Is it possible to combine MultipleLocator and MaxNLocator? One seems
to erase the effect of the other.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy,
Perhaps you might find it useful to look at the Python class that Justin Talbot
wrote (http://www.justintalbot.com/research/axis-labeling/). I find this very
useful in my own work.
Loading...