Nameerror: name sns is not defined

Trying to fix the Python nameerror: name sns is not defined error message…

However, you are struggling to fix it? Then, keep on reading.

In this article, we’ll show how to fix nameerror name sns is not defined in simple and easy way.

What is “sns” module?

The “sns” module is the abbreviation for the seaborn library, which is a Python data visualization library based on matplotlib.

This module provides a high-level interface for drawing attractive and informative statistical graphics.

In addition to that, this library is commonly imported with the shorthand sns, like the following:

 ✅ import seaborn as sns

What is “nameerror name sns is not defined”?

The error message nameerror: name sns is not defined occurs when you’re trying to use the “sns” module without importing it first or you did not install the seaborn library.

For example:

import matplotlib.pyplot as plt


plt.plot([1, 2, 3, 4, 5])
sns.set_style("whitegrid")  # This line will cause the error
plt.show()

If you try to run this code, it will throw a NameError that indicates name sns is not defined.

This error message indicates that you’re trying to use a variable or function named sns.

However, the Python interpreter does not recognize what sns is, because it is not defined or imported.

Why does this error occur?

The nameerror name sns is not defined error message can be raised if you forget to define a variable or if you’re trying to use a library like seaborn (which is commonly imported as sns) but haven’t imported it correctly.

How to fix “nameerror: name sns is not defined”?

To fix the Python nameerror name ‘sns’ is not defined error, you need to ensure that the seaborn library is installed and it is imported correctly and assigned to the variable “sns.”

Here are the following solutions that you use to finally resolved the error.

Solution 1: Install seaborn library

When you haven’t installed the seaborn library yet, you can use the “pip install” command to install the library in your environment.

Here are the steps to install the seaborn library:

  1. Go to Anaconda Command Prompt.
  2.  Input activate tensorflow or conda activate.
  3.  Then, install the seaborn.

You can also install it directly in your command prompt or terminal. Here’s the command that you can use to install the seaborn library:

pip install seaborn

Solution 2: Import seaborn as sns

After the seaborn library is installed you may now Import seaborn and assign it to the variable sns.

You can do this by adding the following line at the beginning of your code.

For example:

import seaborn as sns
import matplotlib.pyplot as plt

# Set the style of the plot
sns.set_style("darkgrid")

# Create some data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Create a plot
plt.plot(x, y)

# Show the plot
plt.show()

As you have noticed, you simply need to import Seaborn as “sns” in your code.

Once you have done that, the “sns.set_style()” function will be recognized by Python, and the code will run without any errors.

Output:

Import seaborn as sns fix the nameerror: name sns is not defined

Solution 3: Import seaborn with a different variable name

If you don’t want to use the variable name sns, you can import seaborn with a different variable name.

For example:

import seaborn as sb
import matplotlib.pyplot as plt

# Set the style of the plot
sb.set_style("darkgrid")

# Create some data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Create a plot
plt.plot(x, y)

You could use import seaborn and assign it to the variable sb to fix the error. The output would be the same as what you can see in the solution 2 above.

Conclusion

In conclusion, the error message nameerror: name sns is not defined occurs when you’re trying to use the “sns” module without importing it first or you did not install the seaborn library.

This article explores what this error is all about and already provides solutions to help you fix this error.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊