Get startedGet started for free

How visualization works in Python

1. How visualization works in Python

Up until now, we have focused on tools for manipulating data. In this chapter, we'll shift gears and take a brief survey of how to visualize data in Python.

2. Importing some more packages

We'll start by importing two packages, seaborn as sns, and matplotlib-dot-pyplot as plt. sns and plt are the standard aliases for these packages. In essence, matplotlib-dot-pyplot allows us to make basic plots, like bar graphs and line graphs, while seaborn is a package built on top of matplotlib-dot-pyplot that makes using matplotlib-dot-pyplot more syntactically and aesthetically pleasing, along with adding some additional plot types which we will not cover in this course.

3. Plotting functions

For this lesson, we'll only focus on one function in each package. The barplot function from the seaborn package will actually create our barplot, and the show function from the matplotlib-dot-pyplot package will instruct Python to display our graphic. In spreadsheets,

4. Spreadsheet graphs

you're probably familiar with something like this: highlighting your data, inserting a chart, seeing a bar plot appear, adjusting some settings in a toolbar, and getting a graphic that looks like this. In Python, it's

5. Python graphs

a little different. We will generally use one function or method, like sns-dot-barplot, to create the instance of a graphic, then another function, plt-dot-show, to properly display the graphic. Here is a screenshot from a Jupyter notebook, a popular tool used for analyzing data with Python. You can see us summarizing fruit-underscore-sales to create the totals DataFrame, calling sns-dot-barplot to first build the graphic, then plt-dot-show to display the graphic. Let's take a closer look at sns-dot-barplot.

6. sns.barplot()

It requires an x argument, which corresponds to the column from our dataset which we wish to display on the x-axis of our plot, in quotes, a y argument, which corresponds to the column from our dataset which we wish to display on the y-axis of our plot, in quotes, and a data argument, which corresponds to the name of our DataFrame variable, in this case totals, with no quotations.

7. plt.show()

On the next line, we simply call the function plt-dot-show to display our graphic. plt-dot-show does not require any arguments. Generally you will be displaying the graphic in some sort of a notebook, like the Jupyter notebook mentioned a few slides back.

8. plt.savefig()

It's also worth mentioning, if you want to save a copy of your plot, you can do so with the plt-dot-savefig function. Savefig just requires a filename of your choosing, and the file extension dot-p-n-g, all in quotations.

9. Your turn!

Now it's your turn to visualize some data.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.