user3429975
user3429975

Reputation: 23

How can I plot from a maximum range to a minimum range in Python

I have some troubles trying to plot a figure in python for a homework I had in university. I want to plot a figure from the maxim range to the minimum range in the x-axis. My code is the next one:

import numpy as np
import matplotlib.pyplot as plt

# function that plots the cummulative histogram
def plotFunction( array , numberBins ):

    # Array of elements that will be plotted on log scale
    elements = array

    # evaluate the histogram
    values, base = np.histogram(elements, bins= len( numberBins ) )
    #evaluate the cumulative
    cumulative = np.cumsum(values)
    # plot the cumulative function
    plt.plot(  cumulative , base[:-1] , c='blue')


    plt.show()

I would like to set the axis the other way around, from 200 to 20.

Upvotes: 1

Views: 1478

Answers (2)

Joe Kington
Joe Kington

Reputation: 284612

One way is to manually set the limits, as @tillsten mentioned. This is the simple, straight-forward way, and if you're just making a static figure, it's what you want.

You can even do plt.xlim(plt.xlim()[::-1]) to avoid having to enter specific ranges.

However, this turns autoscaling off as a side-effect. If you're going to be adding more to the plot (perhaps interactively), you may want to use ax.invert_xaxis() instead.

In your case, you're calling the plot methods through pyplot (instead of as methods of the axes), so you'll need to grab the axes instance first. E.g.

plt.gca().invert_xaxis()

Upvotes: 1

tillsten
tillsten

Reputation: 14878

Use the xlim function or the set_xlim method of the axes:

plt.xlim(200, 20)

Upvotes: 2

Related Questions