Matplotlib Tutorial 5 – stack plots

In this Matplotlib data visualization tutorial, we cover how to create stack plots. The idea of stack plots is to show “parts to the whole” over time. A stack plot is basically like a pie-chart, only over time.

Let’s consider a situation where we have 24 hours in a day, and we’d like to see how we’re spending out time. We’ll divide our activities into: Sleeping, eating, working, and playing.

sample code: http://pythonprogramming.net
http://hkinsley.com
http://sentdex.com
http://seaofbtc.com

Source

Comment List

• sentdex
November 29, 2020

I like how you effortlessly added all the hours up to 24 meanwhile I'm over here with 14 hour days on my chart..

• sentdex
November 29, 2020

This is ever best tutorial for coding

• sentdex
November 29, 2020

I literally wonder what is going on.
0:02

• sentdex
November 29, 2020

For newer versions of MPL, your line for plt.stackplot should look something like:
plt.stackplot(days, sleeping, eating, working, playing, labels=["days","sleeping","eating","working","playing"]) – worked for me!

• sentdex
November 29, 2020

When you get older, the time you spend sleeping and playing decreases and the time you spend working and eating increases.

• sentdex
November 29, 2020

you can use labels easily like this

d=[1,2,3,4,5]
sleep=[7,8,6,11,7]
eating=[2,3,4,3,2]
working=[7,8,7,2,2]
playing=[8,5,7,8,13]
plt.stackplot(d,sleep,eating,working,playing,colors=['m','b','k','r'],labels=['sleep','eating','working','playing'])
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('This is the Title')

• sentdex
November 29, 2020

When I run the code at @3:06 I get an error saying "No handles with labels found to put in legend.", any idea on how to fix this???

• sentdex
November 29, 2020

1:28 "I don't know what a typical human sleeps", neither me pal

• sentdex
November 29, 2020

i have that"line 5236, in fill_between

map(np.ma.getmask, [x, y1, y2]))

ValueError: operands could not be broadcast together with shapes (6,) (5,) "

• sentdex
November 29, 2020

Hey man, this is good stuff! Thanks so much for making this <3<3 ^^

• sentdex
November 29, 2020

On newer versions on matplotib, there is option for labels. reference –https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/stackplot_demo.html

• sentdex
November 29, 2020

Awesome bro

• sentdex
November 29, 2020

I am using this for data visualization. Which is the best one for it?

• sentdex
November 29, 2020

I loved this workaround when you can't label the fills from the stackplot.

• sentdex
November 29, 2020

What is the meaning of [ ] square brackets in plt.plot()
What does it refer
Thanks

• sentdex
November 29, 2020

.show() is not working, working fine when I remove .show()

• sentdex
November 29, 2020

stackplot() got multiple values for argument 'x' i got this error

• sentdex
November 29, 2020

stack plot in not working in my matplot lib version 3.0.0

• sentdex
November 29, 2020

"thiccer"

• sentdex
November 29, 2020

Your channel is wonderful… i dont even know where to begin because everything is so useful. Sticking w/ these matplots for now. Kind of new to python… long time BI developer.

• sentdex
November 29, 2020

Sleeping eating working playing, HARDER BETTER FASTER STRONGER

• sentdex
November 29, 2020

How can I plot scatter plot in real time with changing data points?
I want to loop through with different data points in each loop and plot a scatter plot in the same space as before.
Any kind of help is appreciated!

• sentdex
November 29, 2020

How to do that with a for loop ?

• sentdex
November 29, 2020

hi, It's pretty messed up that no one is polite enough to tell you it's pronounced "muh-jen-ta", anyways you're the man and thanks for uploading this content as well as the copious amounts of other videos and comments!

• sentdex
November 29, 2020

2018, problems solved code 🙂

import numpy as np
import matplotlib.pyplot as plt

days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]

y = np.vstack([sleeping, eating, working, playing])

fig, ax = plt.subplots()
ax.stackplot(days, sleeping, eating, working, playing, labels=['sleeping',
'eating', 'working', 'playing'])

plt.title('Interesting GraphnCheck it out')
plt.legend()

plt.show()

• sentdex
November 29, 2020

#this code works in recent syntax of stackplot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [1, 1, 2, 3, 5]
y2 = [0, 4, 2, 6, 8]
y3 = [1, 3, 5, 7, 9]

labels = ["Region 1", "Region 2", "Region 3"]

fig, ax = plt.subplots()
ax.stackplot(x, y1, y2, y3, labels=labels, colors = ['r','g','b'])
ax.legend(loc='upper left')
plt.show()

• sentdex
November 29, 2020

TypeError: stackplot() got multiple values for argument 'x'

The syntax is outdated. Now this code doesn't seem to work.

• sentdex
November 29, 2020

I have used my own code and the sample code but i keep getting the error: stackplot() got multiple values for argument 'x'. Do you know what may be causing this?

• sentdex
November 29, 2020

days = [1,2,3,4,5]

sleeping =[7,8,6,11,7]

eating = [2,3,4,3,2]

working= [7,8,7,2,2]

playing = [8,5,7,8,13]

plt.stackplot(days, sleeping,eating,working,playing)

plt.xlabel('x')

plt.ylabel('y')

plt.legend(['sleeping','eating','working','playing'])

plt.show()

"""This works totally well."""

• sentdex
November 29, 2020

Easier code that work with new matplotlib (AD 2018).

days = [1, 2, 3, 4, 5]

sleeping = [7, 8, 6, 11, 7]
eating = [2, 3, 4, 3, 2]
working = [7, 8, 7, 2, 2]
playing = [8, 5, 7, 8, 13]

plt.stackplot(days, sleeping, eating, working, playing,
colors=['m', 'c', 'r', 'k'],
labels=['sleeping', 'eating', 'working', 'playing'])
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph')

plt.legend()
plt.show()

• sentdex
November 29, 2020

My somewhat stupid hairsplitting. This video title should be "Matplotlib Tutorial 5 – Stack plots".

• sentdex
November 29, 2020

the best!

• sentdex
November 29, 2020

"I get all mine from the sun" lol

• sentdex
November 29, 2020

Wow man you are killing it when it comes to explaining. Way better than the site tutorial for like 90% of stuff thanks!

• sentdex
November 29, 2020

Just started learning Matplotlib. These videos are really useful. Thanks!

• sentdex
November 29, 2020

hey…. can you suggest me how to draw cummulative frequency graph??

• sentdex
November 29, 2020

well, want to ask what this two empty box plt.plot([],[],color='m', label='Sleeping', linewidth=5) how it help to draw that digram

• sentdex
November 29, 2020

Thannks sentdex, for this unique series.

• sentdex
November 29, 2020

I am not getting the box which shows label on running the program

• sentdex
November 29, 2020

How to show coordinate in each point on a graph

• sentdex
November 29, 2020

I am beginning to learn Python. Your ttutorials are very illuminating. Thanks.

• sentdex
November 29, 2020

In my case it didn't work in any way except when I included the list in the legend -> plt.legend(['sleeping','eating','working','playing']).

• sentdex
November 29, 2020

hi, why plt.colorbar() is not working here?

• sentdex
November 29, 2020

Correct me if I am wrong but cant we just use the label like this:
plt.stackplot(days,sleeping,working,eating,playing,colors = ['m','c','r','k'],labels = ['sleeping','working','eating','playing'])
it works fine in my program.