How does plt.show() know what to show?

后端 未结 1 1582
面向向阳花
面向向阳花 2020-12-12 08:16

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 mat

相关标签:
1条回答
  • 2020-12-12 08:43

    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:

    ^            
    |  *         
    |   **       
    |     *      
    | *     *    
    |            
    +----------->
    
    0 讨论(0)
提交回复
热议问题