6

I would like to show only the relevant parts on the x-axis in a matplotlib generated plot - using the pyplot wrapper library.

Question: How can the plot be forced to cutoff at certain axis-positions, by passing tuples of interval-ranges, that define the intervals for the x-axis to be plotted. Ideally the cutoff should be signified with a vertical double-waved sign superimposed on the x-axis.

Using xlim and axis was of no use, as it only allows the beginning and end of the x-axis to be set but no intervals in-between:

enter image description here Specifically, for the plot above, the x-axis region between 60 to 90 should not be shown, and cutoff/ discontinuous-plot marks should be added.

import matplotlib.pyplot as pyplot
x1, x2 = 0, 50
y1, y2 = 0, 100
pyplot.xlim([x1, x2])
#alternatively
pyplot.axis([x1, x2, y1, y2])

Using matplotlib is not a requirement.

Update/Summary:

  • Viktor points to this source, using subplots-splitting and two plots to emulate a broken-line plot in matplotlib/pyplot.

enter image description here

malla
  • 1,618
  • 1
  • 17
  • 23
Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
  • 1
    @SaulloCastro Thanks for the pointer. So this issue seems unresolved in matplotlib then? However this question encompasses pointers to other nifty plotting libraries. I forgot to update the subject-title... – Lorenz Lo Sauer Aug 31 '13 at 19:00
  • I founds bits and pieces about other plotting libraries here... [What is the best plotting library for Python?](http://stackoverflow.com/questions/1120542/what-is-the-best-plotting-library-for-python) – Lorenz Lo Sauer Aug 31 '13 at 19:42

2 Answers2

5

You have an example of the broken axis in the matplotlib examples: Broken Axis

In your example, subplots would just share the y axis instead of the x axis, and limits would be set on the x axis.

Example with a bar plot:

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
x = [1, 2, 3, 4, 5, 51, 52, 53, 54, 55]
y = [4, 3, 4, 5, 4, 3, 4, 5, 6, 4]
ax1.bar(x, y)
ax2.bar(x, y)

# Fix the axis
ax1.spines['right'].set_visible(False)
ax1.yaxis.tick_left()
ax2.spines['left'].set_visible(False)
ax2.yaxis.tick_right()
ax2.tick_params(labelleft='off')
ax1.set_xlim(1, 6)
ax2.set_xlim(51, 56)
Viktor Kerkez
  • 45,070
  • 12
  • 104
  • 85
-1

If you use the matplotlib.pyplot.xticks you can control the location and value of all the marks.

This answer should show you how to do it.

Community
  • 1
  • 1
Mike Vella
  • 10,187
  • 14
  • 59
  • 86
  • Thanks, but specifically, I do not want the x-axis region between 60-90 to be show. The ticks aren't of primary importance... – Lorenz Lo Sauer Aug 31 '13 at 18:56
  • @LoSauer if you set the ticks so that 60-90 aren't there and leave out the corresponding y-axis data (0s in your case) this will be the result. Consult the link that Saullo sent you. It looks like this question has already been answered there. – Mike Vella Aug 31 '13 at 18:58
  • The links point to workarounds using a `subplots` -table with two plots. Perhaps there is a higher abstracted wrapper library to matplotlib available in Python than pyplot? – Lorenz Lo Sauer Aug 31 '13 at 19:17