简体   繁体   中英

How to make Plt.plot show my parabolic line in python?

Maybe this will be duplicate question but I couldn't find any solution for this. Normally what I coded should show me a curved line in python. But with this code I cant see it. Is there a problem with my code or pycharm ? This code only shows me an empty graphic with the correct axes.

And I did adding "ro" in plt.plot(at[i], st, "ro"). This showed me the spots on the graph but what I want to see the complete line.


at = [0,1,2,3,4,5,6]

for i in range(len(at)):
    st = at[i]**2
    plt.plot(at[i], st)
plt.show()

This is how you would normally do this:

import numpy as np
import matplotlib.pyplot as plt                                                                                                                                                                                                                  
at = np.array([0,1,2,3,4,5,6])
at2 = at ** 2
plt.plot(at,at2)
plt.show()

you can use something like plt.plot(at,at2, c='red', marker='o') to see the spots.

for detailed explanation please read the documentation.

Maybe rather calculate the to be plotted values entirely before plotting.

at = [0,1,2,3,4,5,6]
y = [xi**2 for xi in at]

plt.plot(at, y)

Or do it alternatively with a function

from math import pow

at = [0,1,2,3,4,5,6]

def parabolic(x):
    return [pow(xi,2) for xi in x]

plt.plot(at, parabolic(at))

both return the following plot: 在此处输入图片说明

first generate both lists, then draw the figure:

at = [0,1,2,3,4,5,6]
st = [key**2 for key in at] 
plt.plot(at,st)

在此处输入图片说明

Note that in this way the order to paint on the figure was done only once, which is more efficient.

the other answers give fixes for your question, but don't tell you why your code is not working.

the reason for not "seeing anything" is that plt.plot(at[i], st) was trying to draw lines between the points you give it. but because you were only ever giving it single values it didn't have anything to draw lines between. as a result, nothing appeared on the plot

when you changed to call plt.plot(at[i], st, 'ro') you're telling it to draw single circles at points and these don't go between points so would appear

the other answers showed you how to pass multiple values to plot and hence matplotlib could draw lines between these values.

one of your comments says "its not parabolic still" and this is because matplotlib isn't a symbolic plotting library. you just give it numeric values and it draws these onto the output device. sympy is a library for doing symbolic computation and supports plotting, eg:

from sympy import symbols, plot

x = symbols('x')

plot(x**2, (x, 0, 6))

does the right thing for me. the current release (1.4) doesn't handle discontinuities, but this will be fixed in the next release

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM