Reputation: 141
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 between the points. I want to be able to choose that myself. For example, my y axis narrates - 0,2.5,5,7.5...etc. I want to change it to 0,2,4,6,8... etc. So is there a way to do that? I have tried searching for solutions. I came across xlim, ylim and scale factor. I tried it but it didn't give me the result that I wanted. I also tried figsize but that didn't do it either. Perhaps the solution is one of them and I didn't understand it better. Not sure. So would really appreciate some light on this.
Here is my code at the moment
from matplotlib import pyplot as plt
list1 = ['a','b','c','d']
list2 = [3,7,10,20]
plt.plot(list1,list2)
plt.show()
Just a basic code of making a graph.
Upvotes: 1
Views: 428
Reputation: 128
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()
``̀
Upvotes: 0
Reputation: 114230
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.ticker import MultipleLocator
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
Upvotes: 1
Reputation: 529
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.
<iframe height="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>
Upvotes: 0