plt.show()如何知道要显示什么? [英] How does plt.show() know what to show?

查看:2783
本文介绍了plt.show()如何知道要显示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题不是关于matplotlib的详细信息,而是一个通用的编程和问题,我正在寻找有关使它在python或matplotlib核心中成为可能的机制的答案.

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()

我想知道如何处理此语句?
python(或matplotlib?)如何知道要绘制什么以及从何处获取数据?
解释器如何处理这些语句?

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?

推荐答案

也许我终于明白了这个问题的重点.当然我们不能在这里解释pyplot,因为那太复杂了,需要一个完整的教程(确实存在).但是我们可以看看pyplot如何以非常简化的方式作为模块工作.

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.

因此,让我们创建最终控制台绘图库myplot. ;-)

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

模块myplot可能如下所示.它具有两个函数scattershow以及两个变量figuresplot. plot将存储要绘制的坐标系. figures将存储我们创建的图形.

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)

调用scatterplot创建一个新图形并将其存储在figures列表中.调用show会从该列表中获取所有数字,并显示它们(在控制台中打印出来).

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).

因此,使用myplot看起来与上面的示例完全一样.

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() 

创建输出:

^            
|  *         
|   **       
|     *      
| *     *    
|            
+----------->

这篇关于plt.show()如何知道要显示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆