Population chart, X-Y

How can I create a population chart where x-axis would showcase the decades and y-axis would show 1-6mil.

What I get now:

My data example:
example

My code:

from pandas.io import excel
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import altair as aa

#set_page_config sets a title on your window.
st.set_page_config(page_title="Data Finland")


#heading
st.markdown("<h1 style='text-align: center;  { font-family: finlandica; } '>Data Finland!</h1><br>", unsafe_allow_html=True)


### --- LOAD DATAFRAME
excel_file = "data.xlsx"

data = pd.read_excel(excel_file,
                    usecols='A:B',
                    parse_dates=True)


st.line_chart(data)

#raw data option
if st.checkbox("Show raw data"):
    st.subheader("Raw data")
    st.write(data)

Hi @ElmeriVincent

Set the year column as the dataframe index, then plot.

You can use any of the more advanced charting APIs (pyplot, plotly_chart, altair_chart, vega_lite_chart, etc.) where you have control of which dataframe series is mapped to which axis. See here.

Also look at these viz components and Plost.

Cheers,
Arvindra

Can you give me an example of the year column as the dataframe index?

data.set_index('Year')

Be sure to look at the Pandas documentation if you’re not familiar with it. For these major Python frameworks the docs are very good.

Yeah I will! not sure why but nothing happened when I added data.set_index('Year') but thanks anyway, imma go spend another night reading docs:D it is what it is.

data = data.set_index('Year')
# OR
data.set_index('Year', inplace=True)
1 Like
data.set_index('Year', inline=True)

just gives an error:D "unexpected keyword argument ‘inline’ "

See: pandas.DataFrame.set_index — pandas 1.3.3 documentation

The key word is inplace (have corrected above).

Another solution is to try out my new library called :tomato:Plost!

pip install plost

and then use it like this:

import plost

plost.line_chart(df, "Year", "Population")
3 Likes

I tried it, kinda like the way it looks. Great work.

When hovering the line of the chart it’s not responding so well because cursor has to be exactly right on that thin line to show the population of that certain year. Do you know how that can be fixed? It is not major issue but to create better user experience I would like it to be a bit more responsive.

Well I did add a year selector for the user.

Thank you, got it working.

1 Like

plost installation won’t work anymore thiago

Tried installing it in school didn’t work, tried installing it on my laptop didn’t work. It only worked once on linux.

Hah looks like the :tomato: emoji in my docstring was the culprit! Try now, please.

This is a known Vega-Lite issue :frowning_face:

same error still

That’s very odd… Can you check the version you’re installing? And paste the error again?

Also: what OS are you on?

I thought I fixed it by changing my python version. I can pip install everything else except plost.

Using Windows 10.
Python 3.9 because 3.10 is broken.

What’s strange is that the error message says Plost 0.2.0 and 0.1.0 have issues, but it doesn’t say anything about 0.2.1, which is the one that has the fix.

How are you installing this? pip install --upgrade plost ?

Ive tried pip install plost, pip install --upgrade plost, honestly any possible way that know of. It would be nice to know if anyone else is having the same issue, can’t find anything about Plost on google (not yet atleast)

I tried to fix that unicode error but didn’t work and also I have UTF-8 encoding installed to visual studio code like everyone else so that charmap error is throwing me off.

(requires-python:>=3.5) is weird too since I have 3.9

I uninstalled plost on my linux laptop and re-installed it successfully. Any windows device I have tried to installed plost with hasn’t worked.

Turns out the :tomato: emoji in the README was the culprit :joy: I’ve created a PR to replace it with :tomato: