How do I plot y=x+i for i = [1:16]?

y=x graph
y=x^2 graph
y=|x| graph
y=-x graph
y=x^3 graph
y=x slope
y = x+1
y/x or x/y

I should be getting 16 individual lines, but the endpoint of the first line back tracks and meets the starting point of the next line. How do I fix this?

a = []
b = []
x = 0
for i in range(1,17):
    for x in range(0,5):
        y=x+i
        a.append(x)
        b.append(y)

fig= plt.figure()
axes=fig.add_subplot(111)
pylab.plot(a, b, '-b')

The problem is that you are trying to plot 16 lines but appending them all to a single list. The solution is to initialize empty lists each time and them plot them inside the for loop so that each list gets plotted as a single line.

You should define the figure only once outside the for loop in this case.

In the below answer, I am removing things which you don't need. For ex. x=0 and axes=fig.add_subplot(111). I am replacing this command by another variant.



Complete working answer:

import matplotib.pyplot as plt

fig, ax = plt.subplots(figsize=(8,6))

for i in range(1,17):
    a = []
    b = []
    for x in range(0,5):
        y=x+i
        a.append(x)
        b.append(y)
    plt.plot(a, b, '-b')
plt.show()       

Simpler version

You can simplify your whole code by making using of a numpy array as

import numpy as np
import matplotib.pyplot as plt

fig, ax = plt.subplots(figsize=(8,6))

a = np.arange(5)

for i in range(1,17):
    plt.plot(a, a+i, '-b')

plt.show()

Graph the Line ( y = x ), Graph the line y=x using the slope and y-intercept. https://www.openalgebra.com/ Graph a Duration: 1:57 Posted: May 19, 2012 See below for a couple of suggestions: We can do this a couple of different ways. Slope intercept form We can rewrite y=x into the general form of a slope-intercept equation y=mx+b, with m=1, b=0: y=1x+0 This gives us the y-intercept of 0 (and so we have the point (0,0)) and slope of 1 - which means that for every point we move to the right, we move 1 up ("slope" ="rise"/"run"=(y_2-y_1)/(x_2-x

As your x values are always the same, you can pass them as a 1D array and your y values as a 2D array. matplotlib will then automatically take care of the colouring:

x = np.linspace(0,5,10)
y0 = i0 = np.arange(17)
y = y0[:,None]+x
plt.plot(x,y.T)
plt.show()

gives this image:

how to graph y = x + 3, Find the values of m m and b b using the form y=mx+b y = m x + b . The slope of the line is the value of m m , and the y-intercept is the value of b b . Any line can be graphed using two points. Select two x values, and plug them into the equation to find the corresponding y values. Compute answers using Wolfram's breakthrough technology & knowledgebase, relied on by millions of students & professionals. For math, science, nutrition, history

As you are appending all the answers in a single list, it assumes all lines are connected. You can try fixing sizes of a and b to 5 and then plot each of the 16 lines after each pass of the inner for loop.

for i in range(1,17):
    for x in range(0,5):
        y=x+i
        a.append(x)
        b.append(y)
        pylab.plot(a, b, '-b')
    a=[]
    b=[]

fig= plt.figure()
axes=fig.add_subplot(111)

pylab.show()

Graph y=x, See below for a couple of suggestions: Explanation: We can do this a couple of different ways. Slope intercept form. We can rewrite y=x into the� Let us plot the simple function y = x for the range of values for x from 0 to 100, with an increment of 5. Create a script file and type the following code − x = [0:5:100]; y = x; plot(x, y) When you run the file, MATLAB displays the following plot − Let us take one more example to plot the function y = x 2. In this example, we will draw

How do you graph the equation y=x?, enter image source here y intercept at (0,0) and slope of −11. Explanation: The equation y=−x can be written in slope intercept form as Graph y=x-8. Use the slope-intercept form to find the slope and y-intercept. Tap for more steps The slope-intercept form is , where is the slope and is the y

How do you graph the line y=-x?, Plot (x,y) = (10,10) on the graph. Draw a straight line between them. There's your graph. The point (x,y) is x units to the right� y= -x Here is how you should approach every graphing problem pick a number for x, say 0 then y=0 now pick x=1 then y=-1 now you have two points (0,0) and (1,-1) (you only need two points to graph a line so plot those points, connect them and you got yourself your graph :)

SOLUTION: how do you graph y=x, Plot a function of two variables as a surface in three-dimensional space. Plot a function in two variables: plot sin x cos y. The equation #y=-x# can be written in slope intercept form as #y=-1/1x+0# making the y intercept at #(0,0)# and the slope #-1/1# Begin by graphing the y intercept at the origin (0,0) and then use the slope down 1 for -1 and over 1 to map the remaining points.

Comments
  • I think you need to initialize a and b before the second loop. Otherwise, you're trying to append your values to a list you haven't defined.
  • Yes, you are right. I meant to say this part of the code should be replaced (the lines above this part should stay the same.)