Python Forum
first time use plot - I get empty graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first time use plot - I get empty graph
#1
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 ,
Reply
#2
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.
Reply
#3
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.
Reply
#4
Do not plot points one at a timr. Plot the entire list with one call to plot()
Reply
#5
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()
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
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 ,
Reply
#7
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to plot the graph as needed drunkenneo 1 6,263 May-26-2021, 11:04 AM
Last Post: cygnus_X1
  How to plot 3D graph of non numerical value? Gevni 0 2,188 Mar-05-2021, 02:50 PM
Last Post: Gevni
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,552 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  Plot Graph quest_ 2 2,085 Jan-18-2021, 07:58 PM
Last Post: quest_
  How to plot intraday data of several days in one plot mistermister 3 2,855 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Bode plot from time series experiment data discus 4 7,209 Jun-20-2020, 07:46 AM
Last Post: discus
  plot multiple employee sales data in a single graph pitanshu 0 1,883 Oct-24-2019, 01:56 PM
Last Post: pitanshu
  Plot multiple csv into one graph problem with visualize linkxxx86 1 5,670 Oct-14-2019, 05:54 PM
Last Post: linkxxx86
  Real time graph of the temperatures and storage linkxxx 3 2,656 Aug-29-2019, 09:44 AM
Last Post: linkxxx
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,886 Jun-12-2019, 03:31 PM
Last Post: SriMekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020