2016-02-25 42 views
2

我在64位Windows 10桌面上使用Windows 7。我想要绘制从我得到了一个代码,是一个图:Plot没有在使用matplotlib的Python中显示

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

所有你需要做的就是调用函数像下面这样的现有代码:

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

+0

啊啊,谢谢!我现在觉得很愚蠢。 – Mrinmoy