Home > OS >  How do I set a value for each column? (Python)
How do I set a value for each column? (Python)

Time:11-12

I want to give a value to the columns for each year

gdp = ['$59,928M','$59,907M','$52,618 M','$61,018M','$85,658M']

How can this be done? I'm completely confused. And how to calculate the growth for each year. I have roughly presented the formula, but I can't express it in code (1 year - 1 previous year = the amount for the year)

This is the country's GDP. I want to calculate the increase for each year. How to calculate the absolute and real growth rate for each year and express it in a table? You can throw stones at least, but I really ask you to help the student :)

import numpy as np
import matplotlib.pyplot as plt

x = [2016,2017,2018,2019,2020] #[59.928,59.907,52.618,61.018,85.658]
y = [59.928,59.907,52.618,61.018,85.658]
fig, ax = plt.subplots()

ax.bar(x, y)

ax.set_facecolor('seashell')
fig.set_facecolor('floralwhite')
fig.set_figwidth(12)
fig.set_figheight(6)

plt.show()

CodePudding user response:

If you want to use the list "gdp" as base for labeling the bars, then you can try this:

Code:

ax.set_xticks(x)
ax.set_xticklabels(gdp)

barplot with gdp-names

To see the change between years, simply use numpys diff:

Code:

np.diff(y)

Output:

array([-2.100e-02, -7.289e 00,  8.400e 00,  2.464e 01])

CodePudding user response:

You have to transform your y list to a compatible type in order to plot it and calculate the growth, in our case the type is float.

code:

z = [float(i[1:-1]) for i in y]
print(z)
growth = [z[i 1]-z[i] for i in range(len(z)-1)]
print(growth)

output:

[50.0, 60.0, 70.0, 80.0, 90.0]
[10.0, 10.0, 10.0, 10.0]
  • Related