# Python Tutorial 6: Using Graphs

## Tutorial Objectives and Overview

Motion graphs are a powerful analytical tool. In this tutorial you will learn how to create motion graphs for analyzing motion in Tychos.

• Learn how to create a Graph object with a title.

• Learn how to plot points on the graph in a specific color.

• Learn how to plot multiple data sets on a graph.

• Learn how to create multiple graphs.

You may have already seen motion graphs for position vs time in your Physics class where the position points are plotted on the vertical axis and the time values are plotted on the horizontal axis. Now you are going to learn how to do this in the simulation tool, Tychos.

Click on the link below to open the Tychos scenario in a new browser tab where you can follow along, or use the embedded frame that contains the scenario below:

Python Tutorial - Motion Graphs

In order to display a motion graph, you must first create a `circle` and get it moving. We are going to create a circle object called `car`. Let's create that in the Initial State pane:

``````# Notice the label_text is actually an emoji
car = circle(pos=vec(40, 0), radius=5, color="white", label_text="🚗")``````

Note that we added a label_text atribute with the value of an emoji. This is another way that you can represent your circles with graphic symbols.

Let's give it a velocity only in the X direction for now. Add this line of code just below the one you wrote:

``car.v = vec(-10, 0)``

The car won't move yet because we haven't told Tychos how to move it, but we will do that soon.

First, you are going to create a graph "object". This is done by typing this command in the Initial State pane:

``g_xpos = graph(title="Name of your graph")``

The above code creates a graph titled "Name of your graph". It is wise to actually have the graph title reflect what it is that you are actually going to plot. In our case it is the "x" position coordinate (dependent variable) versus the time (independent variable), so go ahead and change the title of the graph now:

``g_xpos = graph(title="X Position vs. Time")``

OK, this changes the title, but notice that the graph is blank. Nothing is actually being plotted. To tell the Tychos to plot points, we have to tell it what to plot on each axis.

## Plotting Points

Before we begin plotting points, let's make sure that the circle - `car` - is actually moving. Go to the Calculations pane and add this line of code:

``car.pos = car.pos + car.v * dt``

This will get `car` moving and now we can plot its position as a function of time.

To plot points on your graph, you need to give it some coordinate points. Because we created a variable called `g_xpos` to represent our graph, we can now tell our graph to plot some points. The basic syntax looks like this:

``g_xpos.plot(x=independent variable, y=dependent variable, color="#ff0000")``

Time is our independent variable, which is represented by the variable `t`. The X position of the `car` is our dependent variable.

We can access just the X or Y dimensions of a circle's position using this syntax:

``````# plot just the x position
g_xpos.plot(x=t, y=car.pos.x, color="#ff0000")``````

The `plot` command always needs to be placed in the Calculations pane because it is actually a repetitive calculation that the simulation tool must perform each frame.

When you run your simulation, you should see that the blank graph should suddenly look similar to this image below:

You can download an image of any graph by clicking on the small camera icon in the top right corner of the graph.

The program places a plotted point based on the rate identified by the slider labled Graph Plot Interval in the Settings tab. Decrease the interval to see more plotted points, and increase the interval to see less points.

### Changing The Plotted Point Colors

Its quite easy to change the colors of the plotted points, you just need to add a color name when you execute the plot command. Change the last line of code so that it looks like this:

``````# Now plot some points...
g_xpos.plot(t, car.pos.x, "red") ``````

### Multiple Plots

You can plot multiple sets of points on one graph. For example, maybe you wanted to plot the X position of another circle - maybe a second car...

In the Initial State Pane, go ahead and create another `circle` - let's say its called `car2`. The code in your Initial State Pane should now look like this:

``````# A second circle
car2 = circle(pos=vec(100, 0), radius=5, color="blue")
car2.v = vec(-20, 0)``````

This defines a second circle that is blue and that is moving at 20 units/s. We just now need to model its movement like the first car. Add the code in the Calculations Pane so that we are updating the position of the second car - `car2` - using its velocity like this:

``car2.pos = car2.pos + car2.v * dt``

Now that we have done that, we just need to add a second set of plotted points. Add this line of code to add those points to the same graph:

``g_xpos.plot(x=t, y=car2.pos.x, color="blue")``

You should now see two sets of points, one red and one blue, being plotted on the graph. You should notice something right away. Which of the plotted lines has a steeper slope? Which one has a negative slope? Does this match what you have learned about motion graphs?

### Making Multiple Graphs

It is very easy to create multiple graphs. You simply have to add a second graph by defining a new variable, much like we do when we create a second circle object. To add a new graph, simply add one more line to the Initial State pane:

``g_ypos = graph(title="Y Position vs. Time")``

The second graph is now identified by the new variable, `g_ypos`. How would you now plot the Y positions of the two particles on this new graph? Here are the remaining goals you should try to achieve:

• Change the velocity of car1 so that the Y component is negative 10.

• Change the velocity of car2 so that the Y component is positive 5.

• Plot the position values of the two cars' Y positions on the graph in "red" and "blue" for the corresponding cars.

You should notice that the slopes of the plotted points in the Y position graph are different because these lines represent the positions of the particles in the Y dimension. One thing you should also notice is that the lines cross, showing you that the Y positions of the particles are shared at a point in time. Is there an intersection for the X position graph too? Notice the time at which the intersection point occurs.

## Conclusion

You should now be able to:

• Create a graph with a title,

• Plot points on the graph in different colors,

• Plot more than one set of points on a graph,

• Create multiple graphs.

Last updated