Is "from matplotlib import pyplot as plt" == "import matplotlib.pyplot as plt"?

Question:

from matplotlib import pyplot as plt

import matplotlib.pyplot as plt 

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

Asked By: megashigger

||

Answers:

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
Answered By: Padraic Cunningham

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.

Answered By: Eric Appelt

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.

Answered By: Priya Bharti

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.

Answered By: Jason Choi

I like import from matplotlib.pyplot as ply over from matplotlib import pyplot as plt because import element is unifor with import pandas as pd

Answered By: Mehdi khan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.