Nikos Athanasiou
Nikos Athanasiou

Reputation: 31509

matplotlib : plot in semilogx while displaying the actual values

I need to print a semilogx graph, only the x-axis should display the values contained in xdata what I do is

import matplotlib.pyplot as plt

def plot_stuff(xdata, ydata):
    """ 
    """
    plt.clf()
    # titles and stuff ... 
    plt.semilogx(xdata, ydata)

    plt.xticks(xdata) # this line won't do the work

    plt.show()

All I get is a graph that along the x-axis displays the values 10^0 ... 10^n. Is there a way to correct this ?

Upvotes: 0

Views: 2369

Answers (2)

fhdrsdg
fhdrsdg

Reputation: 10532

As the pyplot.xticks() doc shows, the first argument to xticks() is the tick locations, the second is the tick labels. Therefore, plt.xticks(xdata) only changes the tick locations, but does not put the tick labels there too.
Because you want the locations and the labels to be the same, you can simply use:

plt.xticks(xdata, xdata)

Upvotes: 4

Julien Spronck
Julien Spronck

Reputation: 15423

Could this be what you are looking for?

import matplotlib.pyplot as plt

def plot_stuff(xdata, ydata):
    """ 
    """
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.semilogx(xdata, ydata)

    ax.set_xticks(xdata)
    ax.set_xticklabels(xdata)

    plt.show()

Upvotes: 1

Related Questions