Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

from matplotlib import pyplot as plt  import matplotlib.pyplot as plt  

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

like image 342
megashigger Avatar asked May 31 '15 13:05

megashigger


People also ask

What is PLT in import matplotlib Pyplot as PLT?

matplotlib. pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

Is Pyplot same as matplotlib?

Matplotlib is the whole package; matplotlib. pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib. PyLab is a convenience module that bulk imports matplotlib. pyplot (for plotting) and NumPy (for Mathematics and working with arrays) in a single name space.

What is import matplotlib in Python?

Matplotlib is a Python library that helps to plot graphs. It is used in data visualization and graphical plotting. To use matplotlib, we need to install it.

What is matplotlib Pyplot library in Python?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.


2 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:

  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.

like image 166
Eric Appelt Avatar answered Oct 11 '22 14:10

Eric Appelt


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 
like image 43
Padraic Cunningham Avatar answered Oct 11 '22 13:10

Padraic Cunningham