1

My question is not about matplotlib in detail, but a general programming and question, and i'm looking for an answer on the mechanisms making this possible in python or matplotlib core.

Let's say I have a scatter plot using the code:

import matplotlib.pyplot as plt
plt.scatter(a,b)
plt.show()

I'm wondering how is this statement handled?
How does python (or matplotlib?) know what to plot and where to get the data?
How are these statement handled by interpreter?

DavidG
  • 24,279
  • 14
  • 89
  • 82
Danii-Sh
  • 447
  • 1
  • 4
  • 18
  • It knows what to plot as you have supplied the data `a` and `b`. [`plt.show()`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show) shows all figures that have been created – DavidG Jan 23 '18 at 10:33
  • you ask about the mechanics of python... read tutorials about it first. Perhaps you can thereafter narrow your questions down a little? – ZF007 Jan 23 '18 at 10:45
  • Similar questions have been asked already, e.g. [this one](https://stackoverflow.com/questions/42593933/how-does-matplotlib-knows-what-to-display-in-this-code) or [this one](https://stackoverflow.com/questions/46450731/how-do-plt-plotx-y-and-plt-show-work-the-way-they-do). It is for sure a valid desire to understand how matplotlib works. On the other hand, it is not clear what is particularly unclear in this case. If `plt.scatter` creates a figure, what is supprising about `plt.show()` showing that figure? It would help here if you could specifically name the problem of understanding. – ImportanceOfBeingErnest Jan 23 '18 at 12:02
  • thanks all for the downvotes, and @DavidG thanks for your comment and edit, i mean what kind of a structure it is? how does it know what to do without any inputs provided? – Danii-Sh Jan 23 '18 at 18:39
  • What kind of inputs are you expecting to give? – DavidG Jan 23 '18 at 19:38
  • @DavidG that's my problem, i don't understand what kind of a structure this is. how is it working without inputs like show this specific plot – Danii-Sh Jan 23 '18 at 19:42
  • The documentation tells you what arguments certains functions need. Matplotlib internally builds the figures for you (axes, ticks etc). You can add certain things to figures once they are created (such as titles etc). If you want to know how _all_ that is done then it's far too broad a question and you would be better off looking through all the docs and the source code. – DavidG Jan 23 '18 at 19:45
  • Maybe one should mention that the code from the question by itself would of course not work, because `a` and `b` are undefined. I hope this is not what you're after here, but one never knows. For this question to be answerable, maybe you clearly state what you do understand and at which specific point you have a problem. – ImportanceOfBeingErnest Jan 23 '18 at 20:00
  • @ImportanceOfBeingErnest thanks ernest, first i don't get the importance of being ernest , secondly, thanks for your help, i will clear my exact problem up in next comment – Danii-Sh Jan 23 '18 at 20:16
  • @DavidG thanks, i wasn't looking for a detailed answer on internal mechanisms of matplotlib , i don't get where does the show METHOD (correct me if i'm wrong) of matplotlib.pyplot object find the data to output? – Danii-Sh Jan 23 '18 at 20:17
  • `plt.show()` has nothing to do with the data. It displays any figure which has been created (using e.g `plt.scatter()`) – DavidG Jan 23 '18 at 20:21
  • @DavidG thanks, your comments were really helpful – Danii-Sh Jan 24 '18 at 05:52

1 Answers1

4

Maybe I finally see the point of this question. Of course we cannot explain pyplot here, because that is much too complicated and would require a complete tutorial (which btw do exist). But we can have a look at how pyplot would work as a module in a very simplified manner.

So let's create myplot, the ultimative console plotting library. ;-)

The module myplot could look as follows. It has two functions, scatter and show and two variables, figures and plot. plot would store our coordinate system to plot to. figures would store the figures we create.

plot = """
^            
|            
|            
|            
|            
|            
+----------->"""

figures =  []

def scatter(X,Y):
    thisplot = list(plot[:])

    for x,y in zip(X,Y):
        thisplot[1+14*(6-y)+x] = "*"
    thisplot = "".join(thisplot)

    figures.append(thisplot)

def show():
    for fig in figures:
        print(fig)

Calling scatter creates a new figure from plot and stores it in the figures list. Calling show takes all figures from that list, and shows them (prints them in the console).

So using myplot would look exactly like the example above.

import myplot as mlt

mlt.scatter([2,3,4,5,6,8],[2,5,4,4,3,2])

mlt.show() 

Creating the output:

^            
|  *         
|   **       
|     *      
| *     *    
|            
+----------->
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • thanks, so plt.show() looks for a special list that is defined in matplotlib that is in charge of storing all my plots. am i correct? thanks for your great and detailed answer – Danii-Sh Jan 24 '18 at 05:53