Plot没有在Python中使用matplotlib显示

时间:2016-02-25 03:32:52

标签: python python-2.7 matplotlib plot anaconda

我在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的新手,所以这对我帮助很大。谢谢!

2 个答案:

答案 0 :(得分:2)

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

Bar :: a -> b -> Quux.Foo a b

所以总代码是这样的:

make_chart_simple_line_chart(plt)

答案 1 :(得分: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()

enter image description here