pandas - stacked bar chart with timeseries data

2024/9/28 9:52:32

I'm trying to create a stacked bar chart in pandas using time series data:

        DATE        TYPE    VOL0   2010-01-01  Heavy   932.6129031   2010-01-01  Light   370.6129032   2010-01-01  Medium  569.4516133   2010-02-01  Heavy   1068.2500004   2010-02-01  Light   341.5357145   2010-02-01  Medium  484.2500006   2010-03-01  Heavy   1090.9032267   2010-03-01  Light   314.419355

X = date, Y = vol, stacks = type

any help greatly appreciated, thankyou.

Answer

Let's use pandas plot:

df = df.set_index('DATE')
#moved the 'DATE' column into the indexdf.index = pd.to_datetime(df.index)
#convert the string 'DATE' column to a datetime dtypedf.set_index('TYPE',append=True)['VOL'].unstack().plot.bar(stacked=True,figsize=(10,8))
#Moved 'TYPE' into the index with 'DATE' then unstacked 'TYPE' to create a dataframe that has 'DATE' as row labels and 'TYPE' as column labels.  And, then used pandas dataframe plot to chart that frame as a vertical bar chart with stacked=True.

enter image description here

https://en.xdnf.cn/q/71276.html

Related Q&A

Get element at position with Selenium

Is it possible to either run or get the same functionality provided by document.elementFromPoint using a Selenium webdriver?

Facing obstacle to install pyodbc and pymssql in ubuntu 16.04

I want to install pyodbc for connection mssql server using sqlalchemy I am googling and tried in several ways like pip install pyodbcFollowed this link Pyodbc installation error on Ubuntu 16.04 with S…

Cross entropy loss suddenly increases to infinity

I am attempting to replicate an deep convolution neural network from a research paper. I have implemented the architecture, but after 10 epochs, my cross entropy loss suddenly increases to infinity. Th…

Converting each element of a list to tuple

to convert each element of list to tuple like following : l = [abc,xyz,test]convert to tuple list: newl = [(abc,),(xyz,),(test,)]Actually I have dict with keys like this so for searching purpose I need…

Python, Zeep response to pandas

I am tryng to conenct to a SOAP webservice and use pandas to put in on a table.Zeep give me this list:[{ssPeca: 103,ssQtd: 1,ssUn: un }, {ssPeca: 291A,ssQtd: 8,ssUn: un }, {ssPeca: 406B,ssQtd: 8,ssUn: …

Adjust the distance only between two subplots in matplotlib

I have 3 subplots (3 rows and 1 column). We can use fig.subplots_adjust(hspace=0.2) to adjust the distance between the subplots. this will change the distance between subplots for all case. How can I h…

Many-to-many multi-database join with Flask-SQLAlchemy

Im trying to make this many-to-many join work with Flask-SQLAlchemy and two MySQL databases, and its very close except its using the wrong database for the join table. Heres the basics... Ive got main_…

Merge two rows in the same Dataframe if their index is the same?

I have created a large Dataframe by pulling data from an Azure database. The construction of the dataframe wasnt simple as I had to do it in parts, using the concat function to add new columns to the d…

How do I use the Postgresql ANY operator in a NOT IN statement

Using Pyscopg2, how do I pass a Python list into an SQL statement using the ANY Operator?Normal Working SQL reads (See SQL Fiddle):SELECT * FROM student WHERE id NOT IN (3);Using Psycopg2 as below:Psy…

pass command-line arguments to runpy

I have two files, one which has a side effect I care about that occurs within the if __name__ == "__main__" guard:# a.py d = {} if __name__ == "__main__":d[arg] = helloThe second fi…