Reputation: 31
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!
Upvotes: 2
Views: 2676
Reputation: 7326
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()
Upvotes: 2
Reputation: 3891
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)
Upvotes: 2