Tutorials
python
+1

Python Seaborn Tutorial For Beginners

This Seaborn tutorial introduces you to the basics of statistical data visualization

Seaborn: Python's Statistical Data Visualization Library

One of the best but also more challenging ways to get your insights across is to visualize them: that way, you can more easily identify patterns, grasp difficult concepts or draw the attention to key elements. When you’re using Python for data science, you’ll most probably will have already used Matplotlib, a 2D plotting library that allows you to create publication-quality figures. Another complimentary package that is based on this data visualization library is Seaborn, which provides a high-level interface to draw statistical graphics.

Today’s post will cover some of the most frequently asked questions users had while they started out working with the Seaborn library. How many of the following questions can you answer correctly?

 

Interested in a course that covers Matplotlib and Seaborn? Take DataCamp’s Introduction to Data Visualization with Python.

Seaborn vs Matplotlib

As you have just read, Seaborn is complimentary to Matplotlib and it specifically targets statistical data visualization. But it goes even further than that: Seaborn extends Matplotlib and that’s why it can address the two biggest frustrations of working with Matplotlib. Or, as Michael Waskom says in the “introduction to Seaborn”: “If matplotlib “tries to make easy things easy and hard things possible”, seaborn tries to make a well-defined set of hard things easy too.”

One of these hard things or frustrations had to do with the default Matplotlib parameters. Seaborn works with different parameters, which undoubtedly speaks to those users that don’t use the default looks of the Matplotlib plots.

Compare the following plots:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgcGFuZGFzIGFzIHBkXG5cbiMgSW5pdGlhbGl6ZSBGaWd1cmUgYW5kIEF4ZXMgb2JqZWN0XG5maWcsIGF4ID0gcGx0LnN1YnBsb3RzKClcblxuIyBMb2FkIGluIGRhdGFcbnRpcHMgPSBwZC5yZWFkX2NzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9td2Fza29tL3NlYWJvcm4tZGF0YS9tYXN0ZXIvdGlwcy5jc3ZcIilcblxuIyBDcmVhdGUgdmlvbGlucGxvdFxuYXgudmlvbGlucGxvdCh0aXBzW1widG90YWxfYmlsbFwiXSwgdmVydD1GYWxzZSlcblxuIyBTaG93IHRoZSBwbG90XG5wbHQuc2hvdygpIn0=
Python Seaborn tutorial
eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgc2VhYm9ybiBhcyBzbnNcblxuIyBMb2FkIHRoZSBkYXRhXG50aXBzID0gc25zLmxvYWRfZGF0YXNldChcInRpcHNcIilcblxuIyBDcmVhdGUgdmlvbGlucGxvdFxuc25zLnZpb2xpbnBsb3QoeCA9IFwidG90YWxfYmlsbFwiLCBkYXRhPXRpcHMpXG5cbiMgU2hvdyB0aGUgcGxvdFxucGx0LnNob3coKSJ9
Python seaborn violinplot

The Matplotlib defaults that usually don’t speak to users are the colors, the tick marks on the upper and right axes, the style,…

The examples above also makes another frustration of users more apparent: the fact that working with DataFrames doesn’t go quite as smoothly with Matplotlib, which can be annoying if you’re doing exploratory analysis with Pandas. And that’s exactly what Seaborn addresses: the plotting functions operate on DataFrames and arrays that contain a whole dataset.

As Seaborn compliments and extends Matplotlib, the learning curve is quite gradual: if you know Matplotlib, you’ll already have most of Seaborn down.

If you feel your matplotlib skills are rusty, check out the following resources:

 

How To Load Data To Construct Seaborn Plots

When you’re working with Seaborn, you can either use one of the built-in data sets that the library itself has to offer or you can load a Pandas DataFrame. In this section, you’ll see how to do both.

Loading A Built-in Seaborn Data Set

To start working with a built-in Seaborn data set, you can make use of the load_dataset() function. To get an overview or inspect all data sets that this function opens up to you, go here. Check out the following example to see how the load_dataset() function works:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuaW1wb3J0IG1hdHBsb3RsaWIucHlwbG90IGFzIHBsdFxuXG4jIExvYWQgaXJpcyBkYXRhXG5pcmlzID0gc25zLmxvYWRfZGF0YXNldChcImlyaXNcIilcblxuIyBDb25zdHJ1Y3QgaXJpcyBwbG90XG5zbnMuc3dhcm1wbG90KHg9XCJzcGVjaWVzXCIsIHk9XCJwZXRhbF9sZW5ndGhcIiwgZGF0YT1pcmlzKVxuXG4jIFNob3cgcGxvdFxucGx0LnNob3coKSJ9
Seaborn Python Tutorial

As an anecdote, it might be interesting for you to know that the import convention sns comes from the fictional character Samuel Norman “Sam” Seaborn on the television serial drama The West Wing. It’s an inside joke by the core developer of Seaborn, namely, Michael Waskom.

Loading Your Pandas DataFrame Getting Your Data

Of course, most of the fun in visualizing data lies in the fact that you would be working with your own data and not the built-in data sets of the Seaborn library. Seaborn works best with Pandas DataFrames and arrays that contain a whole data set.

Remember that DataFrames are a way to store data in rectangular grids that can easily be overviewed. Each row of these grids corresponds to measurements or values of an instance, while each column is a vector containing data for a specific variable. This means that a DataFrame’s rows do not need to contain, but can contain, the same type of values: they can be numeric, character, logical, etc. Specifically for Python, DataFrames come with the Pandas library, and they are defined as a two-dimensional labeled data structures with columns of potentially different types.

The reason why Seaborn is so great with DataFrames is, for example, because labels from DataFrames are automatically propagated to plots or other data structures, as you saw in the first example of this tutorial, where you plotted a violinplot with Seaborn. There, you saw that the x-axis had a legend total_bill, while this was not the case with the Matplotlib plot. This already takes a lot of work away from you.

But that doesn’t mean that all the work is done -quite the opposite. In many cases, you’ll need to still manipulate your Pandas DataFrame so that the plot will render correctly. If you want to know more, check out DataCamp’s Pandas Tutorial on DataFrames in Python or the Pandas Foundations course.

How To Show Seaborn Plots

Matplotlib still underlies Seaborn, which means that the anatomy of the plot is still the same and that you’ll need to use plt.show() to make the image appear to you. You might have already seen this from the previous example in this tutorial. In any case, here’s another example where the show() function is used to show the plot:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyaWx5IGxpYnJhcmllc1xuaW1wb3J0IG1hdHBsb3RsaWIucHlwbG90IGFzIHBsdFxuaW1wb3J0IHNlYWJvcm4gYXMgc25zXG5cbiMgTG9hZCBkYXRhXG50aXRhbmljID0gc25zLmxvYWRfZGF0YXNldChcInRpdGFuaWNcIilcblxuIyBTZXQgdXAgYSBmYWN0b3JwbG90XG5nID0gc25zLmZhY3RvcnBsb3QoXCJjbGFzc1wiLCBcInN1cnZpdmVkXCIsIFwic2V4XCIsIGRhdGE9dGl0YW5pYywga2luZD1cImJhclwiLCBwYWxldHRlPVwibXV0ZWRcIiwgbGVnZW5kPUZhbHNlKVxuICAgICAgICAgICAgICAgICAgIFxuIyBTaG93IHBsb3RcbnBsdC5zaG93KCkifQ==

Note that in the code chunk above you work with a built-in Seaborn data set and you create a factorplot with it. A factorplot is a categorical plot, which in this case is a bar plot. That’s because you have set the kind argument to "bar". Also, you set which colors should be displayed with the palette argument and that you set the legend to False.

How To Use Seaborn With Matplotlib Defaults

As you read in the introduction, the Matplotlib defaults are something that users might not find as pleasing than the Seaborn defaults. However, there are also many questions in the opposite direction, namely, those use Seaborn and that want to plot with Matplotlib defaults.

Before, you could solve this question by importing the apionly module from the Seaborn package. This is now deprecated (since July 2017). The default style is no longer applied when Seaborn is imported, so you’ll need to explicitly call set() or one or more of set_style(), set_context(), and set_palette() to get either Seaborn or Matplotlib defaults for plotting.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IE1hdHBsb3RsaWJcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcblxuIyBDaGVjayB0aGUgYXZhaWxhYmxlIHN0eWxlc1xucGx0LnN0eWxlLmF2YWlsYWJsZVxuXG4jIFVzZSBNYXRwbG90bGliIGRlZmF1bHRzXG5wbHQuc3R5bGUudXNlKFwiY2xhc3NpY1wiKSJ9

How To Use Seaborn’s Colors As A colormap in Matplotlib?

Besides using Seaborn with Matplotlib defaults, there’s also questions on how to bring in Seaborn colors into Matplotlib plots. You can make use of color_palette() to define a color map that you want to be using and the number of colors with the argument n_colors. In this case, the example will assume that there are 5 labels assigned to the data points that are defined in data1 and data2, so that’s why you pass 5 to this argument and you also make a list with length equal to N where 5 integers vary in the variable colors.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgc2VhYm9ybiBhcyBzbnNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBudW1weSBhcyBucFxuZnJvbSBtYXRwbG90bGliLmNvbG9ycyBpbXBvcnQgTGlzdGVkQ29sb3JtYXBcblxuIyBEZWZpbmUgYSB2YXJpYWJsZSBOXG5OID0gNTAwXG5cbiMgQ29uc3RydWN0IHRoZSBjb2xvcm1hcFxuY3VycmVudF9wYWxldHRlID0gc25zLmNvbG9yX3BhbGV0dGUoXCJtdXRlZFwiLCBuX2NvbG9ycz01KVxuY21hcCA9IExpc3RlZENvbG9ybWFwKHNucy5jb2xvcl9wYWxldHRlKGN1cnJlbnRfcGFsZXR0ZSkuYXNfaGV4KCkpXG5cbiMgSW5pdGlhbGl6ZSB0aGUgZGF0YVxuZGF0YTEgPSBucC5yYW5kb20ucmFuZG4oTilcbmRhdGEyID0gbnAucmFuZG9tLnJhbmRuKE4pXG4jIEFzc3VtZSB0aGF0IHRoZXJlIGFyZSA1IHBvc3NpYmxlIGxhYmVsc1xuY29sb3JzID0gbnAucmFuZG9tLnJhbmRpbnQoMCw1LE4pXG5cbiMgQ3JlYXRlIGEgc2NhdHRlciBwbG90XG5wbHQuc2NhdHRlcihkYXRhMSwgZGF0YTIsIGM9Y29sb3JzLCBjbWFwPWNtYXApXG5cbiMgQWRkIGEgY29sb3IgYmFyXG5wbHQuY29sb3JiYXIoKVxuXG4jIFNob3cgdGhlIHBsb3RcbnBsdC5zaG93KCkifQ==

Tip: do you need to revise NumPy? Consider this NumPy Tutorial or the NumPy cheat sheet.

How To Scale Seaborn Plots For Other Contexts

If you need your plots for talks, posters, on paper or in notebooks, you might want to have larger or smaller plots. Seaborn has got you covered on this. You can make use of set_context() to control the plot elements:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuXG4jIFJlc2V0IGRlZmF1bHQgcGFyYW1zXG5zbnMuc2V0KClcblxuIyBTZXQgY29udGV4dCB0byBgXCJwYXBlclwiYFxuc25zLnNldF9jb250ZXh0KFwicGFwZXJcIilcblxuIyBMb2FkIGlyaXMgZGF0YVxuaXJpcyA9IHNucy5sb2FkX2RhdGFzZXQoXCJpcmlzXCIpXG5cbiMgQ29uc3RydWN0IGlyaXMgcGxvdFxuc25zLnN3YXJtcGxvdCh4PVwic3BlY2llc1wiLCB5PVwicGV0YWxfbGVuZ3RoXCIsIGRhdGE9aXJpcylcblxuIyBTaG93IHBsb3RcbnBsdC5zaG93KCkifQ==

The four predefined contexts are "paper", "notebook", "talk" and "poster". Tip: try changing the context in the DataCamp Light chunk above to another context to study the effect of the contexts on the plot.

You can also pass more arguments to set_context() to scale more plot elements, such as font_scale or more parameter mappings that can override the values that are preset in the Seaborn context dictionaries. In the following code chunk, you overwrite the values that are set for the parameters font.size and axes.labelsize:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuXG4jIFNldCBjb250ZXh0IHRvIGBcInBhcGVyXCJgXG5zbnMuc2V0X2NvbnRleHQoXCJwYXBlclwiLCBmb250X3NjYWxlPTMsIHJjPXtcImZvbnQuc2l6ZVwiOjgsXCJheGVzLmxhYmVsc2l6ZVwiOjV9KVxuXG4jIExvYWQgaXJpcyBkYXRhXG5pcmlzID0gc25zLmxvYWRfZGF0YXNldChcImlyaXNcIilcblxuIyBDb25zdHJ1Y3QgaXJpcyBwbG90XG5zbnMuc3dhcm1wbG90KHg9XCJzcGVjaWVzXCIsIHk9XCJwZXRhbF9sZW5ndGhcIiwgZGF0YT1pcmlzKVxuXG4jIFNob3cgcGxvdFxucGx0LnNob3coKSJ9

Note that in the first code chunk, you have first done a reset to get the default Seaborn parameters back. You did this by calling set(). This is extremely handy if you have experimented with setting other parameters before, such as the plot style.

Additionally, it’s good to keep in mind that you can use the higher-level set() function instead of set_context() to adjust other plot elements:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuXG4jIFJlc2V0IGRlZmF1bHQgcGFyYW1zXG5zbnMuc2V0KHJjPXtcImZvbnQuc2l6ZVwiOjgsXCJheGVzLmxhYmVsc2l6ZVwiOjV9KVxuXG4jIExvYWQgaXJpcyBkYXRhXG5pcmlzID0gc25zLmxvYWRfZGF0YXNldChcImlyaXNcIilcblxuIyBDb25zdHJ1Y3QgaXJpcyBwbG90XG5zbnMuc3dhcm1wbG90KHg9XCJzcGVjaWVzXCIsIHk9XCJwZXRhbF9sZW5ndGhcIiwgZGF0YT1pcmlzKVxuXG4jIFNob3cgcGxvdFxucGx0LnNob3coKSJ9

 

One of the hardest things about data visualizations is customizing the graphs further until they meet your expectations and this stays the same when you’re working with Seaborn. That’s why it’s good to keep in mind the anatomy of the Matplotlib plot and also what this means for the Seaborn library.

As for Seaborn, you have two types of functions: axes-level functions and figure-level functions. The ones that operate on the Axes level are, for example, regplot(), boxplot(), kdeplot(), …, while the functions that operate on the Figure level are lmplot(), factorplot(), jointplot() and a couple others.

This means that the first group is identified by taking an explicit ax argument and returning an Axes object, while the second group of functions create plots that potentially include Axes which are always organized in a “meaningful” way. The Figure-level functions will therefore need to have total control over the figure so you won’t be able to plot an lmplot onto one that already exists. When you call the Figure-level functions, you always initialize a figure and set it up for the specific plot it’s drawing.

You can easily see this when you make a boxplot and an lmplot, for example:

>>> sns.boxplot(x="total_bill", data=tips)
<matplotlib.axes._subplots.AxesSubplot object at 0x117e8da20>

>>> sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})
<seaborn.axisgrid.FacetGrid object at 0x11fa03438>

However, you see that, once you’ve called lmplot(), it returns an object of the type FacetGrid. This object has some methods for operating on the resulting plot that know a bit about the structure of the plot. It also exposes the underlying figure and array of axes at the FacetGrid.fig and FacetGrid.axes arguments.

When you’re customizing your plots, this means that you will prefer to make customizations to your regression plot that you constructed with regplot() on Axes level, while you will make customizations for lmplot() on Figure level.

Let’s see how this works in practice by covering some of the following, most frequently asked questions:

How To Temporarily Set The Plot Style

You can use axes_style() in a with statement to temporarily set the plot style. This, in addition to the use of plt.subplot(), will allow you to make figures that have differently-styled axes, like in the example below:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuXG4jIExvYWQgZGF0YVxuaXJpcyA9IHNucy5sb2FkX2RhdGFzZXQoXCJpcmlzXCIpXG50aXBzID0gc25zLmxvYWRfZGF0YXNldChcInRpcHNcIilcblxuIyBTZXQgYXhlcyBzdHlsZSB0byB3aGl0ZSBmb3IgZmlyc3Qgc3VicGxvdFxud2l0aCBzbnMuYXhlc19zdHlsZShcIndoaXRlXCIpOlxuICAgIHBsdC5zdWJwbG90KDIxMSlcbiAgICBzbnMuc3dhcm1wbG90KHg9XCJzcGVjaWVzXCIsIHk9XCJwZXRhbF9sZW5ndGhcIiwgZGF0YT1pcmlzKVxuXG4jIEluaXRpYWxpemUgc2Vjb25kIHN1YnBsb3RcbnBsdC5zdWJwbG90KDIxMilcblxuIyBQbG90IHZpb2xpbnBsb3RcbnNucy52aW9saW5wbG90KHggPSBcInRvdGFsX2JpbGxcIiwgZGF0YT10aXBzKVxuXG4jIFNob3cgdGhlIHBsb3QgICAgICAgICAgICAgICAgICAgXG5wbHQuc2hvdygpIn0=

How To Set The Figure Size in Seaborn

For axes level functions, you can make use of the plt.subplots() function to which you pass the figsize argument.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuaW1wb3J0IG1hdHBsb3RsaWIucHlwbG90IGFzIHBsdFxuXG4jIEluaXRpYWxpemUgRmlndXJlIGFuZCBBeGVzIG9iamVjdFxuZmlnLCBheCA9IHBsdC5zdWJwbG90cyhmaWdzaXplPSgxMCw0KSlcblxuIyBMb2FkIGluIHRoZSBkYXRhXG5pcmlzID0gc25zLmxvYWRfZGF0YXNldChcImlyaXNcIilcblxuIyBDcmVhdGUgc3dhcm1wbG90XG5zbnMuc3dhcm1wbG90KHg9XCJzcGVjaWVzXCIsIHk9XCJwZXRhbF9sZW5ndGhcIiwgZGF0YT1pcmlzLCBheD1heClcblxuIyBTaG93IHBsb3RcbnBsdC5zaG93KCkifQ==

For Figure-level functions, you rely on two parameters to set the Figure size, namely, size and aspect:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNucyBcblxuIyBMb2FkIGRhdGFcbnRpdGFuaWMgPSBzbnMubG9hZF9kYXRhc2V0KFwidGl0YW5pY1wiKVxuXG4jIFNldCB1cCBhIGZhY3RvcnBsb3RcbmcgPSBzbnMuZmFjdG9ycGxvdChcImNsYXNzXCIsIFwic3Vydml2ZWRcIiwgXCJzZXhcIiwgZGF0YT10aXRhbmljLCBraW5kPVwiYmFyXCIsIHNpemU9NiwgYXNwZWN0PTIsIHBhbGV0dGU9XCJtdXRlZFwiLCBsZWdlbmQ9RmFsc2UpXG5cbiMgU2hvdyBwbG90XG5wbHQuc2hvdygpIn0=

How To Rotate Label Text in Seaborn

To rotate the label text in a Seaborn plot, you will need to work on the Figure level. Note that in the code chunk below, you make use of one of the FacetGrid methods, namely, set_xticklabels, to rotate the text label:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgc2VhYm9ybiBhcyBzbnMgXG5pbXBvcnQgbnVtcHkgYXMgbnBcbmltcG9ydCBwYW5kYXMgYXMgcGRcblxuIyBJbml0aWFsaXplIHRoZSBkYXRhXG54ID0gMTAgKiogbnAuYXJhbmdlKDEsIDEwKVxueSA9IHggKiAyXG5kYXRhID0gcGQuRGF0YUZyYW1lKGRhdGE9eyd4JzogeCwgJ3knOiB5fSlcblxuIyBDcmVhdGUgYW4gbG1wbG90XG5ncmlkID0gc25zLmxtcGxvdCgneCcsICd5JywgZGF0YSwgc2l6ZT03LCB0cnVuY2F0ZT1UcnVlLCBzY2F0dGVyX2t3cz17XCJzXCI6IDEwMH0pXG5cbiMgUm90YXRlIHRoZSBsYWJlbHMgb24geC1heGlzXG5ncmlkLnNldF94dGlja2xhYmVscyhyb3RhdGlvbj0zMClcblxuIyBTaG93IHRoZSBwbG90XG5wbHQuc2hvdygpIn0=

How To Set xlim or ylim in Seaborn

For a boxplot, which works at the Axes level, you’ll need to make sure to assign your boxplot to a variable ax, which will be a matplotlib.axes._subplots.AxesSubplot object, as you saw above. With the object at Axes level, you can make use of the set() function to set xlim, ylim,… Just like in the following example:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG5lY2Vzc2FyeSBsaWJyYXJpZXNcbmltcG9ydCBzZWFib3JuIGFzIHNuc1xuaW1wb3J0IG1hdHBsb3RsaWIucHlwbG90IGFzIHBsdFxuXG4jIExvYWQgdGhlIGRhdGFcbnRpcHMgPSBzbnMubG9hZF9kYXRhc2V0KFwidGlwc1wiKVxuXG4jIENyZWF0ZSB0aGUgYm94cGxvdFxuYXggPSBzbnMuYm94cGxvdCh4PVwidG90YWxfYmlsbFwiLCBkYXRhPXRpcHMpXG5cbiMgU2V0IHRoZSBgeGxpbWBcbmF4LnNldCh4bGltPSgwLCAxMDApKVxuXG4jIFNob3cgdGhlIHBsb3RcbnBsdC5zaG93KCkifQ==

Note that alternatively, you could have also used ax.set_xlim(10,100) to limit the x-axis.

Now, for functions at Figure-level, you can access the Axes object with the help of the axes argument. Let’s see how you can use the ax argument to your advantage to set the xlim and ylim properties:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgc2VhYm9ybiBhcyBzbnMgXG5pbXBvcnQgbnVtcHkgYXMgbnBcbmltcG9ydCBwYW5kYXMgYXMgcGRcblxuIyBJbml0aWFsaXplIHRoZSBkYXRhXG54ID0gMTAgKiogbnAuYXJhbmdlKDEsIDEwKVxueSA9IHggKiAyXG5kYXRhID0gcGQuRGF0YUZyYW1lKGRhdGE9eyd4JzogeCwgJ3knOiB5fSlcblxuIyBDcmVhdGUgbG1wbG90XG5sbSA9IHNucy5sbXBsb3QoJ3gnLCAneScsIGRhdGEsIHNpemU9NywgdHJ1bmNhdGU9VHJ1ZSwgc2NhdHRlcl9rd3M9e1wic1wiOiAxMDB9KVxuXG4jIEdldCBob2xkIG9mIHRoZSBgQXhlc2Agb2JqZWN0c1xuYXhlcyA9IGxtLmF4XG5cbiMgVHdlYWsgdGhlIGBBeGVzYCBwcm9wZXJ0aWVzXG5heGVzLnNldF95bGltKC0xMDAwMDAwMDAwLClcblxuIyBTaG93IHRoZSBwbG90XG5wbHQuc2hvdygpIn0=

Likewise, FacetGrid exposes the underlying figure with the help of the fig argument.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgc2VhYm9ybiBhcyBzbnMgXG5pbXBvcnQgbnVtcHkgYXMgbnBcbmltcG9ydCBwYW5kYXMgYXMgcGRcblxuIyBJbml0aWFsaXplIHRoZSBkYXRhXG54ID0gMTAgKiogbnAuYXJhbmdlKDEsIDEwKVxueSA9IHggKiAyXG5kYXRhID0gcGQuRGF0YUZyYW1lKGRhdGE9eyd4JzogeCwgJ3knOiB5fSlcblxuIyBDcmVhdGUgbG1wbG90XG5sbSA9IHNucy5sbXBsb3QoJ3gnLCAneScsIGRhdGEsIHNpemU9NywgdHJ1bmNhdGU9VHJ1ZSwgc2NhdHRlcl9rd3M9e1wic1wiOiAxMDB9KVxuXG4jIEFjY2VzcyB0aGUgRmlndXJlXG5maWcgPSBsbS5maWcgXG5cbiMgQWRkIGEgdGl0bGUgdG8gdGhlIEZpZ3VyZVxuZmlnLnN1cHRpdGxlKCd0aGlzIGlzIHRoZSBmaWd1cmUgdGl0bGUnLCBmb250c2l6ZT0xMilcblxuIyBTaG93IHRoZSBwbG90XG5wbHQuc2hvdygpIn0=

How To Set Log Scale

You can modify the scale of your axes to better show trends. That’s why it might be useful in some cases to use the logarithmic scale on one or both axes. For a simple regression with regplot(), you can set the scale with the help of the Axes object.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgc2VhYm9ybiBhcyBzbnMgXG5pbXBvcnQgbnVtcHkgYXMgbnBcbmltcG9ydCBwYW5kYXMgYXMgcGRcblxuIyBDcmVhdGUgdGhlIGRhdGFcbnggPSAxMCAqKiBucC5hcmFuZ2UoMSwgMTApXG55ID0geCAqIDJcbmRhdGEgPSBwZC5EYXRhRnJhbWUoZGF0YT17J3gnOiB4LCAneSc6IHl9KVxuXG4jIEluaXRpYWxpemUgZmlndXJlIGFuZCBheFxuZmlnLCBheCA9IHBsdC5zdWJwbG90cygpXG5cbiMgU2V0IHRoZSBzY2FsZSBvZiB0aGUgeC1hbmQgeS1heGVzXG5heC5zZXQoeHNjYWxlPVwibG9nXCIsIHlzY2FsZT1cImxvZ1wiKVxuXG4jIENyZWF0ZSBhIHJlZ3Bsb3RcbnNucy5yZWdwbG90KFwieFwiLCBcInlcIiwgZGF0YSwgYXg9YXgsIHNjYXR0ZXJfa3dzPXtcInNcIjogMTAwfSlcblxuIyBTaG93IHBsb3RcbnBsdC5zaG93KCkifQ==

When you’re working with Figure level functions, you can set the xscale and yscale properties with the help of the set() method of the FacetGrid object:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNucyBcblxuIyBMb2FkIGRhdGFcbnRpdGFuaWMgPSBzbnMubG9hZF9kYXRhc2V0KFwidGl0YW5pY1wiKVxuXG4jIFNldCB1cCBhIGZhY3RvcnBsb3RcbmcgPSBzbnMuZmFjdG9ycGxvdChcImNsYXNzXCIsIFwic3Vydml2ZWRcIiwgXCJzZXhcIiwgZGF0YT10aXRhbmljLCBraW5kPVwiYmFyXCIsIHNpemU9NiwgcGFsZXR0ZT1cIm11dGVkXCIsIGxlZ2VuZD1GYWxzZSlcblxuIyBTZXQgdGhlIGB5c2NhbGVgXG5nLnNldCh5c2NhbGU9XCJsb2dcIilcblxuIyBTaG93IHBsb3RcbnBsdC5zaG93KCkifQ==

How To Add A Title

To add titles to your Seaborn plots, you basically follow the same procedure as you have done in the previous sections. For Axes-level functions, you’ll adjust the title on the Axes level itself with the help of set_title(). Just pass in the title that you want to see appear:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBsaWJyYXJpZXNcbmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHRcbmltcG9ydCBzZWFib3JuIGFzIHNucyBcblxudGlwcyA9IHNucy5sb2FkX2RhdGFzZXQoXCJ0aXBzXCIpXG5cbiMgQ3JlYXRlIHRoZSBib3hwbG90XG5heCA9IHNucy5ib3hwbG90KHg9XCJ0b3RhbF9iaWxsXCIsIGRhdGE9dGlwcylcblxuIyBTZXQgdGl0bGVcbmF4LnNldF90aXRsZShcImJveHBsb3RcIilcblxuIyBTaG93IHRoZSBwbG90XG5wbHQuc2hvdygpIn0=

For Figure-level functions, you can go via fig, just like in the factorplot that you have made in one of the previous sections, or you can also work via the Axes:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IHRoZSBuZWNlc3NhcnkgbGlicmFyaWVzXG5pbXBvcnQgbWF0cGxvdGxpYi5weXBsb3QgYXMgcGx0XG5pbXBvcnQgc2VhYm9ybiBhcyBzbnMgXG5pbXBvcnQgbnVtcHkgYXMgbnBcbmltcG9ydCBwYW5kYXMgYXMgcGRcblxuIyBMb2FkIHRoZSBkYXRhXG50aXBzID0gc25zLmxvYWRfZGF0YXNldChcInRpcHNcIilcblxuIyBDcmVhdGUgc2NhdHRlciBwbG90c1xuZyA9IHNucy5GYWNldEdyaWQodGlwcywgY29sPVwic2V4XCIsIHJvdz1cInNtb2tlclwiLCBtYXJnaW5fdGl0bGVzPVRydWUpXG5nLm1hcChzbnMucGx0LnNjYXR0ZXIsIFwidG90YWxfYmlsbFwiLCBcInRpcFwiKVxuXG4jIEFkZCBhIHRpdGxlIHRvIHRoZSBmaWd1cmVcbmcuZmlnLnN1cHRpdGxlKFwidGhpcyBpcyBhIHRpdGxlXCIpXG5cbiMgU2hvdyB0aGUgcGxvdFxucGx0LnNob3coKSJ9

Data Visualization in Python

Congratulations! You have finished this Seaborn tutorial for beginners.

If you are interested in interactive visualizations, check out DataCamp’s Interactive Data Visualization with Bokeh course! In this course, you’ll learn how to create diverse, rich, data-driven, and interactive visualizations with Bryan Van de Ven, developer of Bokeh and software engineer at Continuum Analytics. While you’re at it, also don’t miss out on DataCamp’s Bokeh cheat sheet.