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?
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
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 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.
-
import matplotlib.pyplot as plt
is shorter but no less clear.
-
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.
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.
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.
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
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
Are the above statements equivalent? Which is more readable/better 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
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 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. -
import matplotlib.pyplot as plt
is shorter but no less clear. -
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.
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.
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.
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