简体   繁体   中英

Plot is not showing in Python using matplotlib

I am using Windows 7 on a 64 bit Windows 10 desktop. 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. 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. 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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