How to Graph in Python | Python Data Science Tutorial

POSTED ON AUGUST 17, 2020
juni logo
How to Graph Survey Data in Python

Hi, my name is Ritika and I’m a senior instructor at Juni Learning! Welcome to this basic Python data science tutorial.

Today we’ll talk about how we can gather data and graph it in Python. Specifically, today we'll be working with survey data that we get beforehand from friends or family by asking what their favorite food is.

We’ll learn how to create bar graphs specifically using dataframes and the Seaborn package.

Who is this for?

  • Juni Level: Python 3
  • Coding Language: Python
  • Coding experience: Intermediate - Advanced
  • Challenge Level: Easy, assistance may be needed in using pandas.
  • Approx lines of code: ~15

Learning outcomes

Core concepts practiced:

  • Beginning data science
  • Graphing data
  • Using dictionaries
  • Using the Pandas package to create a dataframe

Prerequisite concepts to know/review:

  • Dictionaries

Demo

Click run to see the project yourself below!

You can also view my project solution code if you get stuck.

Keep in mind:

We’ll be using dictionaries, so it’ll be important to see the relationship between dictionaries and dataframes, and how we can use data frames to graph things.

General order of steps to implement:

  1. Gather data from family or friends, and put in in a dictionary.
  2. Turn the dictionary into a data frame.
  3. Graph the data.
  4. Save the figure.
  5. We can change some of the features of the graph as well.

How do we do each of these steps?

Step 1: Gather data from family and put in in a dictionary.

Ask around to see what peoples’ favorite foods are. It’s a good idea to have general categories so that we can have an easier time collecting data.

Hint: For example, instead of asking for specific flavors, we can ask people if they like pizza or ice cream and have them pick. Make sure to keep track of your data in one place. We’ll need this for the next step!

Create a dictionary using the data. The key will be a string which will be the food. The value will be the total number of people who voted the food as their favorite.

Step 2: Turn the dictionary into a data frame.

Before we continue, import pandas, seaborn, and matplotlib.pyplot at the top of your code. This will ensure we have all the packages we need for our next step.

Convert the dictionary into a dataframe. We’ll use the DataFrame() function in pandas to complete this step.

Step 3: Graph the data and save the figure.

We’ll be creating a bar plot to display our data; let’s use the barplot() function and our dataframe to complete this. Remember, we have to save our figure as well.

Step 4: We can change some of the features of the graph as well.

Now we have a bar graph. Let’s see if we can change the title and the axes titles. Let’s save out barplot object in a different variable and use set() to change our titles.

Now that we’ve changed our axes let’s take a look. Our bar graph is complete!

Want more of a challenge? Try adding these bonus features.

Extra features:

  • Changing the color scheme of the graph
  • Adding titles to the axes

Creative suggestions:

  • This technique can be used for any survey – try it on other survey data!

Great job — now check out more tutorials!

Thanks for watching and hope you had fun making this project with me!

Built the project above? We'd love to see it! If you're interested in sharing your coding project or experiences with diversity in STEM, please reach out to us at hello@learnwithjuni.com.

Every week, we’ll be posting project tutorials like this one, for different coding languages and experience levels, as well as math tutorials.

Visit our coding projects blog page to find our other tutorials in more coding languages! You can also subscribe to our newsletter using the form to the right of this page for more updates on our programs and newest coding tutorials.

Need more help, or want to keep learning?

A Juni instructor teaching Python.
A Juni Instructor teaches basic Python to a young student.

Looking up your coding questions is one of the best ways to learn!

Another great way to learn is from an experienced coder or instructor.

Juni Learning Computer Science Instructors like Ritika work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.

Read more about our coding courses for kids and curriculum, or speak with a Juni Advisor by calling (650) 263-4306 or emailing advisors@learnwithjuni.com.


Related Reading