Stack Over
Stack Over

Reputation: 425

Plot some numbers in x axis

My question is related to the figure format generated using the matplotlib library.

So, in the x axis, I have the integers from 1 to 100, but in my code I plot just the data related to some numbers (e.g 1,5,77,86,97).

At the end, my code shows me a figure with x axis from 1 to 100.

How can I show just the needed numbers (1,5,77,86,97)?

My code is shown below:

def Plot(self,x,y, steps, nb,bool):  
    if(bool == False):

        labelfont = {
                'family' : 'sans-serif',  # (cursive, fantasy, monospace, serif)
                'color'  : 'black',       # html hex or colour name
                'weight' : 'normal',      # (normal, bold, bolder, lighter)
                'size'   : 20,            # default value:12
                }

        titlefont = {
                'family' : 'serif',
                'color'  : 'black',
                'weight' : 'bold',
                'size'   : 20,
                }

        x = np.array(x)
        y = np.array(y)
        pylab.plot(x, y,
                 'darkmagenta',                       # colour
                 linestyle='',                      # line style
                 linewidth=10,                       # line width
                 marker = '+',
                 mew=6, ms=12)

        axes = plt.gca()
        axes.grid(True)
        axes.set_xlim([1, x_max)            # x-axis bounds
        axes.set_ylim([1, nb])              # y-axis bounds

        pylab.title('Title' , fontdict=titlefont)
        pylab.xlabel('x', fontdict=labelfont)
        pylab.ylabel('y', fontdict=labelfont)

        pylab.subplots_adjust(left=0.15)        # prevents overlapping of the y label
    else:
        test_steps.insert(0,"")
        pylab.yticks(nb,  steps,rotation=0, size=10)
        pylab.margins(0.2)
        pylab.xticks(np.arange(0, x_max, 1.0),rotation=90, size=10)
        # Tweak spacing to prevent clipping of tick-labels
        pylab.subplots_adjust(bottom=0.15)
        F = pylab.gcf()
        DefaultSize = F.get_size_inches()
        if x_max < 100 and len(steps) < 100:
            Xx = 2.5
            Yy = 2.5
        elif x_max < 200 and len(steps) < 200:
            Xx = 5
            Yy = 5
        elif ix_max < 300 and len(steps) < 300:
            Xx = 8
            Yy = 8
        elif x_max < 400 and steps < 400:
            Xx = 10
            Yy = 10
        elif x_max < 500 and steps < 500:
            Xx = 12
            Yy = 12
        else:
            Xx = 15
            Yy = 15
        F.set_size_inches((DefaultSize[0]*Xx , DefaultSize[1]*Yy))
        F.savefig('Out.png', bbox_inches='tight')
        pylab.close(F)

Upvotes: 3

Views: 11556

Answers (3)

Ohad Eytan
Ohad Eytan

Reputation: 8464

That will do:

import matplotlib.pyplot as plt
x = y = list(range(100))
idx = [1,5,77,86,97]
new_y = [y[i] for i in idx]
plt.plot(range(len(idx)), new_y, 'o-')
plt.xticks(range(len(idx)),idx)
plt.show()

output

If you want only dots just remove the -:

plt.plot(range(len(idx)), new_y, 'o')
#                              ---^--- instead of 'o-'

Upvotes: 4

M.T
M.T

Reputation: 5231

If you have a matplotlib.Axes object you can modify the ticks along the x-axis with set_xticks:

import matplotlib.pyplot as plt
plt.plot([0,1,5,77,86,97],range(6),'o-')
ax = plt.gca()
ax.set_xticks([1,5,77,86,97])

with output:

enter image description here

EDIT: assuming you want to plot the values equally separated (though at this point you really should be considering something like bar graphs with the numbers as labels), you could just make a fake x-axis and rename the ticks using set_xticklabels:

import matplotlib.pyplot as plt
x = range(5)
y = range(5)
real_x = [1,5,77,86,97]
plt.plot(x,y,'o-')
ax = plt.gca()
ax.set_xticks(x)
ax.set_xticklabels(real_x)

enter image description here

Upvotes: 2

JE_Muc
JE_Muc

Reputation: 5784

How about masking x with y and then plotting it? like:

pylap.plot(x[y != 0], y,
             'darkmagenta',                       # colour
             linestyle='',                      # line style
             linewidth=10,                       # line width
             marker = '+',
             mew=6, ms=12)

Or if y contains more numbers != 0 and you only want to plot those listed above, just mask x with these numbers...

Upvotes: 0

Related Questions