Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an axis interval range using Matplotlib or other libraries in python [duplicate]

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

like image 393
Lorenz Lo Sauer Avatar asked Aug 31 '13 18:08

Lorenz Lo Sauer


1 Answers

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)
like image 194
Viktor Kerkez Avatar answered Sep 18 '22 06:09

Viktor Kerkez