Skip to content
Advertisement

matplotlib pyplot.plt.style works in Terminal, but not in SublimeText, PyCharm, or BBEdit

I’m having a lot of trouble with matplotlib, specifically pyplot.style.use(). What I have.

First, some sample code, adapted from Python Crash Course 2e.

inputvalues = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter (2, 4, s=200)
plt.show()

When entered one line at the time on the python terminal, everything works as expected. However, when run through any of my Python editors, I get errors.

In Sublime Text, the plt.style.use('seaborn') is flagged and the console says

Traceback (most recent call last):
  File "<file path>", line 6, in <module>
    plt.style.use('seaborn')
AttributeError: 'module' object has no attribute 'style'

In Pycharm, the error seems more fundamental. It triggers on the very first line of the script saying

Traceback (most recent call last):
  File "<filepath>", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

BBedit has a similar but distinct matplotlib error:

Traceback (most recent call last):
  File "<file path>", line 3, in <module>
    plt.style.use('seaborn')
NameError: name 'plt' is not defined

This variety of errors is beyond my n00b python powers to debug.

Advertisement

Answer

You are probably running different python environments without meaning to. One environment is activated in the terminal – that one has matplotlib installed. Another one is used in PyCharm – that one doesn’t.

So the root cause isn’t about Python coding, it’s about getting each app to point to the same python executable. Compare the executable path in your PyCharm config at Preferences | Project | Project Interpreter with the one you have in Terminal. The Python executable set for Terminal can be found by running echo `which python`.

Advertisement