0 votes

I am trying to fix how python plots my data.

Say

```x = [0,5,9,10,15]
```

and

```y = [0,1,2,3,4]
```

Then I would do:

```matplotlib.pyplot.plot(x,y)
matplotlib.pyplot.show()
```

and the x axis' ticks are plotted in intervals of 5. Is there a way to make it show intervals of 1? Jan 5 in Python 1,442 views

## 1 answer to this question.

0 votes

You could explicitly set where you want to tick marks with plt.xticks:

```plt.xticks(np.arange(min(x), max(x)+1, 1.0))
```

For example,

```import numpy as np
import matplotlib.pyplot as plt

x = [0,5,9,10,15]
y = [0,1,2,3,4]
plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.show()
```

(np.arange was used rather than Python's range function just in case min(x) and max(x) are floats instead of ints.)

The plt.plot (or ax.plot) function will automatically set default x and y limits. If you wish to keep those limits, and just change the stepsize of the tick marks, then you could use ax.get_xlim() to discover what limits Matplotlib has already set.

```start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, stepsize))
```

The default tick formatter should do a decent job rounding the tick values to a sensible number of significant digits. However, if you wish to have more control over the format, you can define your own formatter. For example,

```ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
```

Here's a runnable example:

```import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

x = [0,5,9,10,15]
y = [0,1,2,3,4]
fig, ax = plt.subplots()
ax.plot(x,y)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, 0.712123))
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
plt.show()``` answered Jan 5 by
• 65,870 points

0 votes
1 answer

## How do I determine if my python shell is executing in 32bit or 64bit mode on OS X?

UPDATED: One way is to look at sys.maxsize as ...READ MORE

0 votes
1 answer

## How can I change the data type to string in Python?

You can use str(variablename) This is called conversion ...READ MORE

0 votes
1 answer

## How to handle AssertionError in Python and find out which line or statement it occurred on?

Use the traceback module: import sys import traceback try: ...READ MORE

+2 votes
1 answer

## How can I record the X,Y limits of a displayed X,Y plot using the matplotlib show() module?

A couple hours after posting this question ...READ MORE

0 votes
1 answer

## how do i change string to a list?

suppose you have a string with a ...READ MORE

0 votes
2 answers

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote
2 answers

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

0 votes
0 answers

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE

0 votes
2 answers

## How to change the font size on a matplotlib plot?

Functions dealing with text like label, title, etc. accept ...READ MORE

0 votes
4 answers

## how to sort a list of numbers without using built-in functions like min, max or any other function?

Yes it is possible. You can refer ...READ MORE