Python Forum

Full Version: first time use plot - I get empty graph
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I want to show variable in a graph , I don't have "x" only the value of y
so I thought to create an int for numbers of time I get the speed and increase it every time

I will explain
I'm reading speed senor and I want to create a speed graph
the function read the data from a known file , return and print it - this part works without any problems.
this is what I have

import matplotlib.pyplot as plt

t = 0
def GetDataFunction()
    global t
    Speed = GetData()
    print(f"Speed: {Speed} Km/h ")
    plt.plot(t, float(Speed))
    t += 1

GetDataFunction()

plt.show()
what did I miss?



***
I have try to make it even more simple

import matplotlib.pyplot as plt
 
t = 0
Speed = [0, 1, 3, 50, 60, 22, 30]

def Function():
    global t
    while t < len(Speed):
      print(t, '-', Speed[t])
      plt.plot(t, Speed[t])
      t += 1
    
Function()
but I get the same result - empty graph
I can see the max value is:
Y - 60
X - 6
so something is right :-)

Thanks ,
Not empty. It has one point. You would see that if you specified a marker.

Put the points in an array or list, then plot the list.
OK
now I have added a marker and I can see the points - thanks ,

can I connected the dots? with a line ?

I have try this
 plt.plot(t, Speed[t], color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
but its the same 7 points in green color with no connection between them.
Do not plot points one at a timr. Plot the entire list with one call to plot()
You could try something like this, but with your data

import matplotlib.pyplot as plt

time = [0, 5, 10, 15, 20, 25, 30]
speed = [0, 1, 3, 50, 60, 22, 30]
plt.xlabel("Time")
plt.ylabel("Speed")
plt.plot(time, speed)
plt.show()
how can I do this?
Quote:how do I Plot the entire list with one call to plot() ?

import matplotlib.pyplot as plt

def GetDataFunction()
   t= 0
   while t <250:
       Speed = GetData()
       print(f"Speed: {Speed} Km/h ")
       plt.plot(t, float(Speed))
       t += 1
 
GetDataFunction()
 
plt.show()
Thanks ,
If you want your plot to update as points are added, use animation.

https://matplotlib.org/stable/api/animation_api.html

If you just want to make a plot, stop adding points in a loop. Make a list of points, and plot the list.
import matplotlib.pyplot as plt
import math

def get_data(t):
    return [math.sin(math.radians(deg)) for deg in range(t)]

plt.plot(get_data(360))
plt.show()