from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
Are the above statements equivalent? Which is more readable/better form?
4 Answers
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:
It is generally customary to use
import matplotlib.pyplot as pltand suggested in the matplotlib documentation (see etc...) so this will be more familiar to most readers.import matplotlib.pyplot as pltis shorter but no less clear.import matplotlib.pyplot as pltgives an unfamiliar reader a hint that pyplot is a module, rather than a function which could be incorrectly assumed from the first form.
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
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.
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.