A Guide to Plotting with Pandas and PyPlot

Analyze top-selling items by selecting the top 5 revenues using n-largest, then plot them as a bar chart with cleaned labels and a title.

Learn how to identify and visualize your top-selling products by leveraging Python's Pandas library for data analysis and visualization. This article demonstrates how to use built-in methods to extract and graph the highest revenue-generating items in a dataset.

Key Insights

  • Use the nlargest() method in Pandas to efficiently retrieve the top 10 highest revenue-generating items from a dataset, making it easy to analyze key performers like the Chicken Bowl.
  • Create bar charts with Pandas' integrated plotting, applying styles such as 'bmh' for improved readability and visual presentation of the data.
  • Noble Desktop’s course illustrates how to refine plots by setting appropriate labels and titles, and filtering data to show only the top five items for clearer comparisons.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Let's experiment with how we can look at this revenue that we made, revenues, and find out what are our top sellers. We made this column, revenues, and which one's highest? There's many different ways we could do that, but one of the ways I like to do it is use n-largest. We could say, okay, I want the top 10, say.

Top 10 by revenue equals our revenues, and I want the n-largest 10. That means, you know, go with this column and find the n-largest values, and we'll look at the top 10 by revenue, and there they are, and they're sorted by the largest. We can see Chicken Bowl is on there, and we're going to take a look at how we can plot that, graph it, and visualize it.

Okay. Plotting them as a bar chart. There are a number of different ways to go about this, and in fact, we're going to do this again using Dash in a moment, but we're going to use PyPlot, but PyPlot as leveraged directly by Pandas.

Now that we have our data in this format, it's already got the X values, the names of them, and their corresponding y values, which is how high the bar should be. We can make it very easy now that we have it in that format to say, okay, go plot that, PyPlot. We can say revenues.plot.bar, and we can style it one of my favorite styles is the BMH style, and you can look up if you don't know these.

There are a lot of different styles you can use, and they're just some CSS style sheets to make your graph look a little fancier. Okay. We can add, let's show it.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Let's see what this looks like. Okay. Well, we got all the revenues.

We didn't really want that, and it's not really very readable. So what we meant to do is the top 10 by revenue. That's the sorted only the top 10 that we made.

So instead of plotting it off of revenues, let's plot it off of top 10 by revenue. There we go. That's looking a lot better.

There's chicken bowl at the top, all the way down to veggie bowl. Now, we probably should get some labels on this. These ones don't really need a label, and they have that weird item name label.

We're going to clean this up a little bit. We'll say plt.xlabel empty string, meaning just don't even give me an X label. And we can add a y label though.

That might be, you know, what should go on here. And maybe it should be item revenue in dollars. And finally, this thing deserves a title.

And this title is lying to you. It says top five items by revenue, and that is in fact what we intended to do. But we have top 10.

That's what we're plotting here. So that's pretty easy though. Instead of just doing top 10 by revenue, we could make a separate top five by revenue.

But let's just take this, and let's say, give me rows zero to five in it. And now we have the actual top five items by revenue graphed. All right, next we're going to dive into, get out of the notebook.

Now that we've explored our data, now that we've played around with it, now that we've come up with something we actually want to make a visualization of, it's time to go make it the real thing with a real Python file.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram