How to set axis spacing range using Matplotlib or other libraries in python

I would like to show only the relevant parts along the x axis in the plotted matplotlib plot - using the pyplot wrapper pyplot .

Question: How to force the plot to be forced to turn off at certain positions of the axes, passing tuples of interval intervals that define the intervals for the x axis to be built. Ideally, cropping should mean a vertical sign with two waves superimposed on the x axis.

Using xlim and axis was useless, since it allows you to set the beginning and end of the x axis, but without intervals in -between:

enter image description here In particular, for the above graph, the x-axis area between 60 to 90 should not be displayed, and cut-off / interrupt 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:

  • Victor points to this source using subplots -splitting and two graphs to emulate a broken line in matplotlib / pyplot.

enter image description here

+6
source share
2 answers

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

In your example, the subheadings will simply separate the y axis instead of the x axis, and the constraints will be set on the x axis.

Example with a line:

 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) 
+4
source

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

This answer should show you how to do this.

-1
source

Source: https://habr.com/ru/post/952893/


All Articles