66
from matplotlib import pyplot as plt

import matplotlib.pyplot as plt 

Are the above statements equivalent? Which is more readable/better form?

0

4 Answers 4

66

Even though they are equivalent, I think there is a pretty good argument that the second form import matplotlib.pyplot as plt is objectively more readable:

  1. It is generally customary to use import matplotlib.pyplot as plt and suggested in the matplotlib documentation (see http://matplotlib.org/users/pyplot_tutorial.html etc...) so this will be more familiar to most readers.

  2. import matplotlib.pyplot as plt is shorter but no less clear.

  3. import matplotlib.pyplot as plt gives an unfamiliar reader a hint that pyplot is a module, rather than a function which could be incorrectly assumed from the first form.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, I'm trying to get a hang of the terminology. Is "pyplot" alone the module and "matplotlib" also a module? Or is "matplotlib.pyplot" the module? Thanks!
matplotlib is a package, essentially a collection of related modules. At its simplest, a package can be just a directory containing the module files with an empty __init__.py file that tells python that the directory is to be treated as a package. A module B within package A (i.e. in the file structure A/B.py is imported as import A.B
@EricAppelt Is the syntax A.B the same with attributes of an object?
15

They both work the same so it is up to you which you prefer, personally I don't like typing so I would prefer the second.

from matplotlib import pyplot as plt

import matplotlib.pyplot as plt1

print(dir(plt) == dir(plt1))
True

Comments

5

Just noticed one case that makes the two statements work differently to me

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')

plt.plot(list(range(10)))

The above code works well. But if I write the second line in the other way,

import matplotlib
from matplotlib import pyplot as plt
matplotlib.use('Qt5Agg')

plt.plot(list(range(10)))

This above doensn't work and the process stops at "matplotlib.use('Qt5Agg')". Process finished with exit code -1073741571 (0xC00000FD)

This happens in IDE like Spyder console or Pycharm console. I feel it's related to the backend used though I didn't have a clear clue.

1 Comment

I see a similar problem in PyCharm 2021.3 and PyCharm 2022: when doing import matplotlib.pyplot as plt it works, but if I do from matplotlib import pyplot as plt I get a gui window that is blank and title Not Responding. My Colleagues also run PyCharm, but do not have this problem.
3

Yes, both are the same.

It depends on you what you prefer to import.

Personally, I like to write:

from matplotlib import pyplot as plt

because it looks clearer and cleaner to me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.