##### matplotlib x axis range goes over the set range
 matplotlib x axis range goes over the set range Pedroski55 Minister of Silly Walks Posts: 478 Threads: 118 Joined: Jul 2017 Reputation: Nov-20-2021, 01:37 AM (This post was last modified: Nov-20-2021, 01:37 AM by Pedroski55.) I am just trying to do some simple plots, nothing fancy. So I am trying y = x**2 I thought I set x in the range -4 to +4 with: x = np.arange(-4, 4, 0.2), but I get range 0 to 40 for x. (I suppose there are 40 0.2s in 8) I would like the zero on the x-axis to coincide with the zero on the y-axis. 2 small problems: 1. How do I set x-axis zero and y-axis zero in the same place? Ideally, x and y zero should be in the centre of the figure, so I see -4 to +4 on both axes. 2. How can I fix the range of x? (I would like a small interval in the range in order to give a smooth curve.) ```def tp(): fig = plt.figure(figsize=(6, 6)) x = np.arange(-4, 4, 0.2) y = x**2 ax = fig.add_subplot(121) #ax2 = fig.add_subplot(122) #ax.fig(num='This is the title') # Move left y-axis and bottom x-axis to centre, passing through (0,0) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('zero') # Eliminate upper and right axes ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # Show ticks in the left and lower axes only ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.set_title('Full view') ax.plot(y, color='blue', label='y = x^2') plt.xlabel("X axis") plt.ylabel("Y = x^2") plt.legend() plt.show()``` Reply deanhystad Black Knight Posts: 3,893 Threads: 15 Joined: Feb 2020 Reputation: Nov-20-2021, 03:39 AM (This post was last modified: Nov-20-2021, 03:39 AM by deanhystad.) x = np.arange(-4, 4, 0.2) returns a list of 40 numbers starting at -4 and ending at 3.8, incrementing by 0.2. If it didn't you wouldn't get the values you are for y. Your problem is you are not plotting x vs y. You are plotting y and matplotlib is providing values for x. Next time you have a problem like this LOOK AT THE VALUES. Print x and y and you would realize they were both correct and the problem must be you weren't doing the plot correctly. Then you would probably notice "Oh, I didn't include x in the plot command. I probably need to do that." Reply Pedroski55 Minister of Silly Walks Posts: 478 Threads: 118 Joined: Jul 2017 Reputation: Nov-20-2021, 06:18 AM Thanks! I took that from an example I found. Thought it must be the way to go. Whoever wrote the example did not put x in ax.plot(x, y, color='blue', label='y = x^2'), just y. I presumed matplotlib used the available x. Still, x-axis zero and y-axis zero do not coincide, any ideas on that?? The y-axis does not run through the tick which indicates x = zero, there is a slight gap. Reply deanhystad Black Knight Posts: 3,893 Threads: 15 Joined: Feb 2020 Reputation: Nov-20-2021, 03:12 PM (This post was last modified: Nov-20-2021, 03:12 PM by deanhystad.) Zero is off center because you have more points left of zero than right of zero. Set your range to -4, 4.1. https://matplotlib.org/stable/api/_as_ge....plot.html Quote:plot(y) # plot y using x as index array 0..N-1 Did you really think that plot somehow surmised that your program, which is completely hidden from plot, contained an array of x axis values that you wanted to use in the plot, and that it should override the documented default behavior of using an index array for x when no x values are provided? Didn't that seem odd? Tutorials are fine as long as you assume half of them are crap. You still have to think and you still need to read the docs. Reply Pedroski55 Minister of Silly Walks Posts: 478 Threads: 118 Joined: Jul 2017 Reputation: Nov-20-2021, 10:50 PM Well, you have to get used to these things. When you've done it a few times, you know the ropes. After I asked the last question, I looked at it again, then I realized what to do. I get what I want now. Anyway, thanks for your tips and advice, you put me on the right road! Reply paul18fr Splitter Posts: 190 Threads: 50 Joined: Apr 2019 Reputation: Nov-21-2021, 08:40 AM Here are some changes. For example you can bracket abcissa values or ordinates ones, or you can also implemente Latex formula, and so on. There are many options ```import matplotlib.pyplot as plt import numpy as np import os x = np.arange(-4., 4.2, 0.2) y = x**2 Path = str(os.path.abspath('')) fig = plt.figure(figsize=(6, 6)) ax = fig.add_subplot(121) #ax2 = fig.add_subplot(122) #ax.fig(num='This is the title') # Move left y-axis and bottom x-axis to centre, passing through (0,0) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('zero') # Eliminate upper and right axes ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # Show ticks in the left and lower axes only ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.set_title('Full view') ax.plot(x, y, color='blue', label=r'Parabola \$y = x^2\$') ax.set(xlim=(-4., 4.), ylim=(0., 20.)) plt.xlabel("X axis") plt.ylabel(r"\$Y = x^2\$") plt.legend() # plt.show() plt.savefig(Path + '/Fig.png',dpi=300)``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Solving an equation by brute force within a range alexfrol86 3 859 Aug-09-2022, 09:44 AM Last Post: Gribouillis IndexError: list index out of range dolac 4 422 Jul-25-2022, 03:42 PM Last Post: deanhystad I'm getting a String index out of range error debian77 7 638 Jun-26-2022, 09:50 AM Last Post: deanhystad IndexError: list index out of range Anldra12 2 597 May-03-2022, 01:39 PM Last Post: Anldra12 TypeError: list indices must be integers or slices, not range Anldra12 2 734 Apr-22-2022, 10:56 AM Last Post: Anldra12 x-axis labels with Matplotlib Mark17 8 790 Mar-23-2022, 06:10 PM Last Post: Mark17 How to make x-axis readable with matplotlib Mark17 7 1,545 Mar-01-2022, 04:30 PM Last Post: DPaul Using multiprocessing to produce objects for i in range lucasrohr 6 777 Feb-02-2022, 03:53 PM Last Post: lucasrohr gspread - applying ValueRenderOption to a range of cells dwassner 0 647 Jan-12-2022, 03:05 PM Last Post: dwassner [Solved] Matplotlib - Tricontour: how to set colorbar range ju21878436312 1 2,309 Dec-13-2021, 07:44 PM Last Post: ju21878436312

Forum Jump:

### User Panel Messages ##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020