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

Yes,Both are same. It's Depends upon you what you prefer to import.

Personally I Like to Write :

from matplotlib import pyplot as plt

Because it's look more clear and clean to me.


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

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.


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.