【问题标题】:Plot is not showing in Python using matplotlib使用 matplotlib 在 Python 中未显示绘图
【发布时间】:2016-06-07 15:52:34
【问题描述】:

我在 64 位 Windows 10 桌面上使用 Windows 7。我正在尝试根据给定的代码绘制图表:

import matplotlib.pyplot as plt
from collections import Counter

def make_chart_simple_line_chart(plt):

    years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
    gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]

    # create a line chart, years on x-axis, gdp on y-axis
    plt.plot(years, gdp, color='green', marker='o', linestyle='solid')

    # add a title
    plt.title("Nominal GDP")

    # add a label to the y-axis
    plt.ylabel("Billions of $")
    plt.show()

我查看了其他问题,但似乎找不到答案,除非我看错了地方。我检查了后端,它是“Qt4Agg”,我认为它应该是正确的后端,但它仍然没有显示。我没有收到任何错误,只是没有显示情节。我对 Python 很陌生,所以这对我有很大帮助。谢谢!

【问题讨论】:

    标签: python python-2.7 matplotlib plot anaconda


    【解决方案1】:

    或者你可以避免函数,

    import matplotlib.pyplot as plt
    
    years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
    gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
    
    # apply 3rd party plot style
    plt.style.use('ggplot')
    
    # create a line chart, years on x-axis, gdp on y-axis
    plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
    
    # add a title
    plt.title("Nominal GDP")
    
    # add a label to the y-axis
    plt.ylabel("Billions of $")
    plt.show()
    

    【讨论】:

      【解决方案2】:

      您需要做的就是在现有代码下面这样调用您的函数:

      make_chart_simple_line_chart(plt)
      

      所以总的代码是这样的:

      import matplotlib.pyplot as plt
      from collections import Counter
      
      def make_chart_simple_line_chart(plt):
      
          years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
          gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
      
          # create a line chart, years on x-axis, gdp on y-axis
          plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
      
          # add a title
          plt.title("Nominal GDP")
      
          # add a label to the y-axis
          plt.ylabel("Billions of $")
          plt.show()
      
      make_chart_simple_line_chart(plt)
      

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 2021-05-13
        • 2015-12-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        • 2017-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多