Skip to content Skip to sidebar Skip to footer

How Do I Choose A Size Between Each Point On Y Axis In Matplotlib?

I am just trying to make a normal graph using a list for the x axis and a list for the y axis. But the thing is, I want to be able to choose the size of the y axis. Like the length

Solution 1:

Matplotlib selects tick locations using a Locator object. You can implement your own locator by extending the class matplotlib.ticker.Locator, or by configuring one of the provided ones. For example, your y-axis could be configured with a MultipleLocator:

from matplotlib import pyplot as plt
from matplotlib.tickerimportMultipleLocator

fig, ax = plt.subplots()
ax.plot(....)
ax.yaxis.set_major_locator(MultipleLocator(2))

Here is a gallery of the main built-in locators to help you select one you like: https://matplotlib.org/gallery/ticks_and_spines/tick-locators.html

Solution 2:

You can use matplotlib.pyplot.ylim with bottom and top parameters like this:

plt.ylim(2,20)

Although it starts from 2 but it could be a workaround.

<iframeheight="400px"width="100%"src="https://repl.it/@pagalprogrammer/y-lim-matplotlib?lite=true"scrolling="no"frameborder="no"allowtransparency="true"allowfullscreen="true"sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

Solution 3:

this can be done by using set_xticks()

import matplotlib.pyplot as plt
import numpy as np

x1 = np.arange(0, 10, 2.5) #generate a list of numbers spaced by 2.5
x2 = np.arange(0, 10, 2) #generates a list of numbers spaced by 2

x0 = np.arange(0, 10, 0.5) #creates something to plot
y0 = x0**2

fig1, ax1 = plt.subplots()
ax1.plot(x0, y0, '-o')
ax1.set_xticks(x1)
plt.show()

fig2, ax2 = plt.subplots()
ax2.plot(x0, y0, '-s')
ax2.set_xticks(x2)
plt.show()
``̀


Post a Comment for "How Do I Choose A Size Between Each Point On Y Axis In Matplotlib?"