【问题标题】:How to fix my plot in following graph in jupyter notebook?如何在 jupyter notebook 的下图中修复我的情节?
【发布时间】:2021-05-23 12:13:44
【问题描述】:

我正在尝试实现如下所示的图表:

我在 Microsoft Excel 中这样做了,但现在我试图在 Jupyter Notebook 中获得相同的结果,但我被卡住了,请指导我正确的方向: 这是我尝试过的:

x_coordinates = [1000, 10000, 100000,250000,500000,1000000]

y1_coordinates = [0.016, 0.096, 0.9893,2.9166,9.2952,21.779]
y2_coordinates = [0.13, 15.215, 557.45,4768.842,55577.73,77777]
plt.xlabel('number of elements(n)') 
plt.plot(x_coordinates, y1_coordinates)

plt.ylabel('time in secs') 
plt.plot(x_coordinates, y2_coordinates)

但它没有给我确切的结果,到目前为止看起来像这样:

【问题讨论】:

    标签: python excel matplotlib graph jupyter-notebook


    【解决方案1】:

    一个问题是 x 轴间距。 Excel 图表中的间距与数字之间的差异不成比例;它不是线性的或对数的。因此,您可以根据整数绘制 y 值,然后在之后重命名 ticks。见here

    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(7,2))
    
    x_coordinates = [1000, 10000, 100000,250000,500000,1000000]
    x_positions = range(len(x_coordinates)) # get int positions
    
    y1_coordinates = [0.016, 0.096, 0.9893,2.9166,9.2952,21.779]
    y2_coordinates = [0.13, 15.215, 557.45,4768.842,55577.73,77777]
    
    # plot against integers
    plt.xlabel('number of elements(n)')
    plt.plot(x_positions, y1_coordinates)
    
    plt.ylabel('time in secs')
    plt.plot(x_positions, y2_coordinates)
    
    # relable with coordinates
    plt.xticks(ticks=x_positions, labels=x_coordinates)
    

    橙色曲线在 x=500000 时看起来仍然不同;但这似乎只是绘制的数据略有不同。

    您也可以通过将x_coordinates 转换为字符串来实现类似的效果:

    # this makes the same graph as above
    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(7,2)) # for size
    
    x_coordinates = [1000, 10000, 100000,250000,500000,1000000]
    x_coordinates = [str(i) for i in x_coordinates]
    
    y1_coordinates = [0.016, 0.096, 0.9893,2.9166,9.2952,21.779]
    y2_coordinates = [0.13, 15.215, 557.45,4768.842,55577.73,77777]
    
    # plot against integers
    plt.xlabel('number of elements(n)')
    plt.plot(x_coordinates, y1_coordinates)
    
    plt.ylabel('time in secs')
    plt.plot(x_coordinates, y2_coordinates)
    

    x轴下方有表格的东西比较独特;我不确定在matplotlib 中是否有一种简单的方法可以轻松实现这一点。如果这更多是您感兴趣的部分,您可以编辑您的问题/创建一个新问题(但 see this postthis example)。

    【讨论】:

      【解决方案2】:

      您也可以使用以下代码实现此目的。

      import matplotlib.pyplot as plt
      from matplotlib.ticker import AutoMinorLocator, FormatStrFormatter
      
      fig, ax = plt.subplots(1, 1)
      x_coordinates = [1000, 10000, 100000,250000,500000,1000000]
      y1_coordinates = [0.016, 0.096, 0.9893,2.9166,9.2952,21.779]
      y2_coordinates = [0.13, 15.215, 557.45,4768.842,55577.73,77777]
      fig.suptitle('Insertion Sort v/s Merge Sort', fontsize=14)
      plt.xlabel('number of elements(n)') 
      plt.ylabel('time in secs') 
      plt.xscale('symlog')
      plt.plot(x_coordinates, y1_coordinates,'b',lw=2)
      plt.plot(x_coordinates, y2_coordinates,'orange',lw=2)
      ax.xaxis.set_major_formatter(FormatStrFormatter("%.f")) # major axis formattter
      plt.grid()
      

      对于表格,我建议遵循@Tom 的建议。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-22
        • 1970-01-01
        • 1970-01-01
        • 2013-09-14
        • 1970-01-01
        • 2021-05-16
        • 2021-10-17
        相关资源
        最近更新 更多