简体   繁体   English

使用matplotlib在Python中未显示图

[英]Plot is not showing in Python using matplotlib

I am using Windows 7 on a 64 bit Windows 10 desktop. 我在64位Windows 10桌面上使用Windows 7。 I am trying to plot a graph from a code that I was given which is: 我正在尝试从给出的代码中绘制图形:

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

I've looked at other questions and I can't seem to find an answer unless I'm looking at the wrong places. 我看过其他问题,除非找到错误的地方,否则似乎找不到答案。 I've checked the backend and it is 'Qt4Agg' which I believe is supposed to be the correct backend but it is still not showing. 我检查了后端,它是“ Qt4Agg”,我认为它应该是正确的后端,但仍未显示。 I'm not getting any error it is just not showing the plot. 我没有任何错误,只是没有显示情节。 I am very new to Python so this would help me out a lot. 我对Python很陌生,所以这对我有很大帮助。 Thank you! 谢谢!

All you need to do is call your function like this below your existing code: 您需要做的就是在现有代码下像这样调用函数:

make_chart_simple_line_chart(plt)

So the total code would be like this: 因此,总代码如下:

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)

Or you can avoid function, 或者你可以避免功能,

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM