Matplotlib图为空,代码看起来很好

时间:2019-02-18 01:46:29

标签: python matplotlib

有人可以帮助我解释为什么我的图表为空白。下面是代码。我正在尝试解决一个问题,即客户去了一家有256便士便士的书店,并计算了以N个人数进行计数所需的时间。

import numpy as np
import math
import matplotlib.pyplot as plt
%matplotlib inline


def bag_count(N, E, T=2):
  """
  N: Number of bags
  E: Number of Employees
  T: Number of bags you can count in 1 second, 
     One person can only add two numbers at a time
  """
    count = (N / T) / E
    print("It took {} secs to count {} bags by {} employees".format(count, N, E))
    plt.plot(N, count, linewidth=2.0)
    plt.axis([0, N, 0, count])
    plt.ylabel("Time it takes to count N bags (t)")
    plt.xlabel("The number of bags (N)")
    plt.show()

def main():    
    bag_count(256,2)

main()

2 个答案:

答案 0 :(得分:1)

绘图中只有一个点,并且使用线选项对其进行绘图,因此没有线。添加标记以查看该点。另外,该点位于您范围的拐角处,因此不容易看到。替换为它可以更轻松地查看:

    plt.plot(N, count, marker='*', linewidth=2.0)
    plt.axis([0, 2*N, 0, 2*count])

答案 1 :(得分:0)

该图并不是真正的“空白”,只是您看不到放入的一个数据点。 您的plot基本上只在图形上放置一个标记,因此,如果您确实要这样做,则应考虑设置标记大小以可视化数据点,如下所示:< / p>

import numpy as np
import math
import matplotlib.pyplot as plt
%matplotlib inline


def bag_count(N, E, T=2):
    """
    N: Number of bags
    E: Number of Employees
    T: Number of bags you can count in 1 second, 
       One person can only add two numbers at a time
    """
    count = (N / T) / E
    print("It took {} secs to count {} bags by {} employees".format(count, N, E))
    plt.plot(N, count, "x", markersize=15)
    #plt.axis([0, N, 0, count])
    plt.ylabel("Time it takes to count N bags (t)")
    plt.xlabel("The number of bags (N)")
    plt.show()

def main():    
    bag_count(256,2)

main()

但是,只有放置多个数据点,您的绘图才有意义。