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

2
  • 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... Commented Aug 31, 2013 at 19:00
  • I founds bits and pieces about other plotting libraries here... What is the best plotting library for Python? Commented Aug 31, 2013 at 19:42

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Link-only answers are not good, as the links may rot.
@nordev Got it. Added an example.
-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.

3 Comments

Thanks, but specifically, I do not want the x-axis region between 60-90 to be show. The ticks aren't of primary importance...
@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.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.