hvplot not displaying dynamically

355 Views Asked by At

The code below works fine

data_2015=(data[data.Year==2015]
    .groupby("Country")
    ["Country", "Life expectancy "]
    .median()
    .sort_values(by="Life expectancy ", ascending=True))

data_2015.reset_index().hvplot.bar(x="Country", y="Life expectancy ", rot=90,width=2000, height=550, title = "Life Expectancy for ALL Countries for 2015")

but when I try to input data_year dynamically with the code below, the plot does not show

year = input ('Life expectancy ranking of what year between 2000-2015 are you interested in?:  ')

data_year=(data[data.Year==year]
   .groupby(["Country"])
    [["Country", "Life expectancy "]]
    .median()
    .sort_values(by="Life expectancy ", ascending=False))

data_year.reset_index().hvplot.bar(x="Country", y="Life expectancy ", rot=90,width=2100, height=500, title ="Life expectancy ranking of countries in 2015")

what am i missing?

1

There are 1 best solutions below

2
Redox On BEST ANSWER

The issue you are facing is due to the input process. When you use input(), the data is read as a string. Convert it to integer using int() and it will work.

Updated code

year = input ('Life expectancy ranking of what year between 2000-2015 are you interested in?:  ')

data_year=(data[data.Year==int(year)]
   .groupby(["Country"])
    [["Country", "Life expectancy "]]
    .median()
    .sort_values(by="Life expectancy ", ascending=False))

myTitle = "Life expectancy ranking of countries in " + year
data_year.reset_index().hvplot.bar(x="Country", y="Life expectancy ", rot=90,width=2100, height=500, title = myTitle)

enter image description here