Difference between revisions of "Matplotlib"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
m
Tag: visualeditor
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Formatter strings ==
+
==Formatter strings==
 
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
 
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
  
==Example==
+
==Cheatsheets==
 +
https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Matplotlib_Cheat_Sheet.pdf
 +
 
 +
https://matplotlib.org/cheatsheets/cheatsheets.pdf
 +
<br />
 +
==Examples==
 +
https://www.youtube.com/watch?v=XFZRVnP-MTU&list=PL-osiE80TeTvipOqomVEeZ1HRrcEvtZB_&index=1
 +
 
 +
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Matplotlib
 +
 
 +
===Lines===
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
 
from matplotlib import pyplot as plt
 
from matplotlib import pyplot as plt
 
plt.xkcd()
 
  
 
ages_x = [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 
ages_x = [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 
           36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]
 
           36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]
 
 
py_dev_y = [20046, 17100, 20000, 24744, 30500, 37732, 41247, 45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640, 84666,
 
py_dev_y = [20046, 17100, 20000, 24744, 30500, 37732, 41247, 45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640, 84666,
 
             84392, 78254, 85000, 87038, 91991, 100000, 94796, 97962, 93302, 99240, 102736, 112285, 100771, 104708, 108423, 101407, 112542, 122870, 120000]
 
             84392, 78254, 85000, 87038, 91991, 100000, 94796, 97962, 93302, 99240, 102736, 112285, 100771, 104708, 108423, 101407, 112542, 122870, 120000]
plt.plot(ages_x, py_dev_y, label='Python')
 
 
 
js_dev_y = [16446, 16791, 18942, 21780, 25704, 29000, 34372, 37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583, 79000,
 
js_dev_y = [16446, 16791, 18942, 21780, 25704, 29000, 34372, 37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583, 79000,
 
             78508, 79996, 80403, 83820, 88833, 91660, 87892, 96243, 90000, 99313, 91660, 102264, 100000, 100000, 91660, 99240, 108000, 105000, 104000]
 
             78508, 79996, 80403, 83820, 88833, 91660, 87892, 96243, 90000, 99313, 91660, 102264, 100000, 100000, 91660, 99240, 108000, 105000, 104000]
plt.plot(ages_x, js_dev_y, label='JavaScript')
 
 
 
dev_y = [17784, 16500, 18012, 20628, 25206, 30252, 34368, 38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752, 77232,
 
dev_y = [17784, 16500, 18012, 20628, 25206, 30252, 34368, 38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752, 77232,
 
         78000, 78508, 79536, 82488, 88935, 90000, 90056, 95000, 90000, 91633, 91660, 98150, 98964, 100000, 98988, 100000, 108923, 105000, 103117]
 
         78000, 78508, 79536, 82488, 88935, 90000, 90056, 95000, 90000, 91633, 91660, 98150, 98964, 100000, 98988, 100000, 108923, 105000, 103117]
 +
 +
 +
# plt.xkcd()  # --> comic style
 +
# print(plt.style.available)
 +
plt.style.use('fivethirtyeight')
 +
plt.plot(ages_x, py_dev_y, linewidth=3, label='Python')
 +
plt.plot(ages_x, js_dev_y, label='JavaScript')
 
plt.plot(ages_x, dev_y, color='#444444', linestyle='--', marker='o' label='All Devs')
 
plt.plot(ages_x, dev_y, color='#444444', linestyle='--', marker='o' label='All Devs')
 +
plt.xlabel('Ages')
 +
plt.ylabel('Median Salary (USD)')
 +
plt.title('Median Salary (USD) by Age')
 +
plt.legend()
 +
plt.grid(True)
 +
plt.tight_layout()
 +
plt.savefig('plot.png')
 +
plt.show()
 +
</syntaxhighlight>
 +
 +
====Fill between====
 +
<syntaxhighlight lang="python3">
 +
import pandas as pd
 +
from matplotlib import pyplot as plt
 +
 +
data = pd.read_csv('data.csv')
 +
ages = data['Age']
 +
dev_salaries = data['All_Devs']
 +
py_salaries = data['Python']
 +
js_salaries = data['JavaScript']
 +
 +
plt.plot(ages, dev_salaries, color='#444444',
 +
        linestyle='--', label='All Devs')
 +
 +
plt.plot(ages, py_salaries, label='Python')
 +
 +
overall_median = 57287
 +
 +
plt.fill_between(ages, py_salaries, dev_salaries,
 +
                where=(py_salaries > dev_salaries),
 +
                interpolate=True, alpha=0.25, label='Above Avg')
 +
 +
plt.fill_between(ages, py_salaries, dev_salaries,
 +
                where=(py_salaries <= dev_salaries),
 +
                interpolate=True, color='red', alpha=0.25, label='Below Avg')
 +
 +
plt.legend()
  
 +
plt.title('Median Salary (USD) by Age')
 
plt.xlabel('Ages')
 
plt.xlabel('Ages')
 
plt.ylabel('Median Salary (USD)')
 
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by Age')
+
 
 +
plt.tight_layout()
 +
 
 +
plt.show()
 +
</syntaxhighlight>
 +
 
 +
===Bar===
 +
 
 +
====Vertical====
 +
<syntaxhighlight lang="python3">
 +
import numpy as np
 +
from matplotlib import pyplot as plt
 +
 
 +
 
 +
ages_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
 +
x_indexes = np.arange(len(ages_x))
 +
width = 0.25
 +
 
 +
dev_y = [38496, 42000, 46752, 49320, 53200,
 +
        56000, 62316, 64928, 67317, 68748, 73752]
 +
plt.bar(x_indexes - width, dev_y, width=width, color="#444444", label="All Devs")
 +
 
 +
py_dev_y = [45372, 48876, 53850, 57287, 63016,
 +
            65998, 70003, 70000, 71496, 75370, 83640]
 +
plt.bar(x_indexes, py_dev_y, width=width, color="#008fd5", label="Python")
 +
 
 +
js_dev_y = [37810, 43515, 46823, 49293, 53437,
 +
            56373, 62375, 66674, 68745, 68746, 74583]
 +
plt.bar(x_indexes + width, js_dev_y, width=width, color="#e5ae38", label="JavaScript")
 +
 
 +
plt.legend()
 +
plt.xticks(ticks=x_indexes, labels=ages_x)
 +
plt.title("Median Salary (USD) by Age")
 +
plt.xlabel("Ages")
 +
plt.ylabel("Median Salary (USD)")
 +
 
 +
plt.tight_layout()
 +
 
 +
plt.show()
 +
</syntaxhighlight>
 +
 
 +
====Horizontal====
 +
<syntaxhighlight lang="python3">
 +
 
 +
import csv
 +
import numpy as np
 +
import pandas as pd
 +
from collections import Counter
 +
from matplotlib import pyplot as plt
 +
 
 +
plt.style.use("fivethirtyeight")
 +
 
 +
data = pd.read_csv('data.csv')
 +
ids = data['Responder_id']
 +
lang_responses = data['LanguagesWorkedWith']
 +
 
 +
language_counter = Counter()
 +
 
 +
for response in lang_responses:
 +
    language_counter.update(response.split(';'))
 +
 
 +
languages = []
 +
popularity = []
 +
 
 +
for item in language_counter.most_common(15):
 +
    languages.append(item[0])
 +
    popularity.append(item[1])
 +
 
 +
languages.reverse()
 +
popularity.reverse()
 +
 
 +
plt.barh(languages, popularity)
 +
 
 +
plt.title("Most Popular Languages")
 +
# plt.ylabel("Programming Languages")
 +
plt.xlabel("Number of People Who Use")
 +
 
 +
plt.tight_layout()
 +
 
 +
plt.show()
 +
</syntaxhighlight>
 +
 
 +
===Pie===
 +
<syntaxhighlight lang="python3">
 +
from matplotlib import pyplot as plt
 +
 
 +
plt.style.use("fivethirtyeight")
 +
 
 +
slices = [59219, 55466, 47544, 36443, 35917]
 +
labels = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java']
 +
explode = [0, 0, 0, 0.1, 0]
 +
 
 +
plt.pie(slices, labels=labels, explode=explode, shadow=True,
 +
        startangle=90, autopct='%1.1f%%',
 +
        wedgeprops={'edgecolor': 'black'})
 +
 
 +
plt.title("My Awesome Pie Chart")
 +
plt.tight_layout()
 +
plt.show()
 +
</syntaxhighlight>
 +
 
 +
===Stack===
 +
<syntaxhighlight lang="python3">
 +
 
 +
from matplotlib import pyplot as plt
 +
 
 +
plt.style.use("fivethirtyeight")
 +
 
 +
 
 +
minutes = [1, 2, 3, 4, 5, 6, 7, 8, 9]
 +
 
 +
player1 = [8, 6, 5, 5, 4, 2, 1, 1, 0]
 +
player2 = [0, 1, 2, 2, 2, 4, 4, 4, 4]
 +
player3 = [0, 1, 1, 1, 2, 2, 3, 3, 4]
 +
 
 +
labels = ['player1', 'player2', 'player3']
 +
colors = ['#6d904f', '#fc4f30', '#008fd5']
 +
 
 +
plt.stackplot(minutes, player1, player2, player3, labels=labels, colors=colors)
 +
 
 +
plt.legend(loc=(0.07, 0.05))
 +
# plt.legend(loc="upper left")
 +
 
 +
 
 +
plt.title("My Awesome Stack Plot")
 +
plt.tight_layout()
 +
plt.show()
 +
</syntaxhighlight>
 +
 
 +
===Histogram===
 +
<syntaxhighlight lang="python3">
 +
import pandas as pd
 +
from matplotlib import pyplot as plt
 +
 
 +
plt.style.use('fivethirtyeight')
 +
 
 +
data = pd.read_csv('data.csv')
 +
ids = data['Responder_id']
 +
ages = data['Age']
 +
 
 +
bins = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
 +
 
 +
plt.hist(ages, bins=bins, edgecolor='black', log=True)
 +
 
 +
median_age = 29
 +
color = '#fc4f30'
 +
 
 +
plt.axvline(median_age, color=color, label='Age Median', linewidth=2)
  
 
plt.legend()
 
plt.legend()
 +
 +
plt.title('Ages of Respondents')
 +
plt.xlabel('Ages')
 +
plt.ylabel('Total Respondents')
 +
 +
plt.tight_layout()
 +
 +
plt.show()
 +
</syntaxhighlight>
 +
 +
===Scatter plot===
 +
<syntaxhighlight lang="python3">
 +
import pandas as pd
 +
from matplotlib import pyplot as plt
 +
 +
plt.style.use('seaborn')
 +
 +
data = pd.read_csv('2019-05-31-data.csv')
 +
view_count = data['view_count']
 +
likes = data['likes']
 +
ratio = data['ratio']
 +
 +
plt.scatter(view_count, likes, c=ratio, cmap='summer',
 +
            edgecolor='black', linewidth=1, alpha=0.75)
 +
 +
cbar = plt.colorbar()
 +
cbar.set_label('Like/Dislike Ratio')
 +
 +
plt.xscale('log')
 +
plt.yscale('log')
 +
 +
plt.title('Trending YouTube Videos')
 +
plt.xlabel('View Count')
 +
plt.ylabel('Total Likes')
 +
 +
plt.tight_layout()
 +
 +
plt.show()
 +
</syntaxhighlight>
 +
 +
===Time Series===
 +
<syntaxhighlight lang="python3">
 +
import pandas as pd
 +
from datetime import datetime, timedelta
 +
from matplotlib import pyplot as plt
 +
from matplotlib import dates as mpl_dates
 +
 +
plt.style.use('seaborn')
 +
 +
data = pd.read_csv('data.csv')
 +
 +
data['Date'] = pd.to_datetime(data['Date'])
 +
data.sort_values('Date', inplace=True)
 +
 +
price_date = data['Date']
 +
price_close = data['Close']
 +
 +
plt.plot_date(price_date, price_close, linestyle='solid')
 +
 +
plt.gcf().autofmt_xdate()  # rotate dates
 +
 +
# date_format = mpl_dates.DateFormatter('%b, %d %Y')
 +
# plt.gca().xaxis.set_major_formatter(date_format)
 +
 +
plt.title('Bitcoin Prices')
 +
plt.xlabel('Date')
 +
plt.ylabel('Closing Price')
 +
 +
plt.tight_layout()
 +
 +
plt.show()
 +
</syntaxhighlight>
 +
 +
===Ploting live data===
 +
<syntaxhighlight lang="python3">
 +
import random
 +
from itertools import count
 +
import pandas as pd
 +
import matplotlib.pyplot as plt
 +
from matplotlib.animation import FuncAnimation
 +
 +
plt.style.use('fivethirtyeight')
 +
 +
x_vals = []
 +
y_vals = []
 +
 +
index = count()
 +
 +
 +
def animate(i):
 +
    data = pd.read_csv('data.csv')
 +
    x = data['x_value']
 +
    y1 = data['total_1']
 +
    y2 = data['total_2']
 +
 +
    plt.cla()
 +
 +
    plt.plot(x, y1, label='Channel 1')
 +
    plt.plot(x, y2, label='Channel 2')
 +
 +
    plt.legend(loc='upper left')
 +
    plt.tight_layout()
 +
 +
 +
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
  
 
plt.tight_layout()
 
plt.tight_layout()
 +
plt.show()
 +
# ----------------------------------------------------------
 +
# Another way to do it without clearing the Axis
 +
from itertools import count
 +
import pandas as pd
 +
import matplotlib.pyplot as plt
 +
from matplotlib.animation import FuncAnimation
 +
 +
plt.style.use('fivethirtyeight')
 +
 +
x_vals = []
 +
y_vals = []
 +
 +
plt.plot([], [], label='Channel 1')
 +
plt.plot([], [], label='Channel 2')
 +
 +
 +
def animate(i):
 +
    data = pd.read_csv('data.csv')
 +
    x = data['x_value']
 +
    y1 = data['total_1']
 +
    y2 = data['total_2']
 +
 +
    ax = plt.gca()
 +
    line1, line2 = ax.lines
 +
 +
    line1.set_data(x, y1)
 +
    line2.set_data(x, y2)
 +
 +
    xlim_low, xlim_high = ax.get_xlim()
 +
    ylim_low, ylim_high = ax.get_ylim()
  
plt.savefig('plot.png')
+
    ax.set_xlim(xlim_low, (x.max() + 5))
 +
 
 +
    y1max = y1.max()
 +
    y2max = y2.max()
 +
    current_ymax = y1max if (y1max > y2max) else y2max
 +
 
 +
    y1min = y1.min()
 +
    y2min = y2.min()
 +
    current_ymin = y1min if (y1min < y2min) else y2min
 +
 
 +
    ax.set_ylim((current_ymin - 5), (current_ymax + 5))
 +
 
 +
 
 +
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
 +
 
 +
plt.legend()
 +
plt.tight_layout()
 +
plt.show()
 +
</syntaxhighlight>
 +
 
 +
===Subplots===
 +
<syntaxhighlight lang="python3">
 +
import pandas as pd
 +
from matplotlib import pyplot as plt
 +
 
 +
plt.style.use('seaborn')
 +
 
 +
data = pd.read_csv('data.csv')
 +
ages = data['Age']
 +
dev_salaries = data['All_Devs']
 +
py_salaries = data['Python']
 +
js_salaries = data['JavaScript']
 +
 
 +
# On same figure:
 +
# fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True)
 +
 
 +
fig1, ax1 = plt.subplots()
 +
fig2, ax2 = plt.subplots()
 +
 
 +
ax1.plot(ages, dev_salaries, color='#444444',
 +
        linestyle='--', label='All Devs')
 +
 
 +
ax2.plot(ages, py_salaries, label='Python')
 +
ax2.plot(ages, js_salaries, label='JavaScript')
 +
 
 +
ax1.legend()
 +
ax1.set_title('Median Salary (USD) by Age')
 +
ax1.set_ylabel('Median Salary (USD)')
 +
 
 +
ax2.legend()
 +
ax2.set_xlabel('Ages')
 +
ax2.set_ylabel('Median Salary (USD)')
 +
 
 +
plt.tight_layout()
  
 
plt.show()
 
plt.show()
 +
 +
fig1.savefig('fig1.png')
 +
fig2.savefig('fig2.png')
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[Category:Python]]
 +
[[Category:DataScience]]

Latest revision as of 21:28, 12 February 2022

Formatter strings[edit]

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html

Cheatsheets[edit]

https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Matplotlib_Cheat_Sheet.pdf

https://matplotlib.org/cheatsheets/cheatsheets.pdf

Examples[edit]

https://www.youtube.com/watch?v=XFZRVnP-MTU&list=PL-osiE80TeTvipOqomVEeZ1HRrcEvtZB_&index=1

https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Matplotlib

Lines[edit]

from matplotlib import pyplot as plt

ages_x = [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
          36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]
py_dev_y = [20046, 17100, 20000, 24744, 30500, 37732, 41247, 45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640, 84666,
            84392, 78254, 85000, 87038, 91991, 100000, 94796, 97962, 93302, 99240, 102736, 112285, 100771, 104708, 108423, 101407, 112542, 122870, 120000]
js_dev_y = [16446, 16791, 18942, 21780, 25704, 29000, 34372, 37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583, 79000,
            78508, 79996, 80403, 83820, 88833, 91660, 87892, 96243, 90000, 99313, 91660, 102264, 100000, 100000, 91660, 99240, 108000, 105000, 104000]
dev_y = [17784, 16500, 18012, 20628, 25206, 30252, 34368, 38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752, 77232,
         78000, 78508, 79536, 82488, 88935, 90000, 90056, 95000, 90000, 91633, 91660, 98150, 98964, 100000, 98988, 100000, 108923, 105000, 103117]


# plt.xkcd()  # --> comic style
# print(plt.style.available)
plt.style.use('fivethirtyeight')
plt.plot(ages_x, py_dev_y, linewidth=3, label='Python')
plt.plot(ages_x, js_dev_y, label='JavaScript')
plt.plot(ages_x, dev_y, color='#444444', linestyle='--', marker='o' label='All Devs')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by Age')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig('plot.png')
plt.show()

Fill between[edit]

import pandas as pd
from matplotlib import pyplot as plt

data = pd.read_csv('data.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']

plt.plot(ages, dev_salaries, color='#444444',
         linestyle='--', label='All Devs')

plt.plot(ages, py_salaries, label='Python')

overall_median = 57287

plt.fill_between(ages, py_salaries, dev_salaries,
                 where=(py_salaries > dev_salaries),
                 interpolate=True, alpha=0.25, label='Above Avg')

plt.fill_between(ages, py_salaries, dev_salaries,
                 where=(py_salaries <= dev_salaries),
                 interpolate=True, color='red', alpha=0.25, label='Below Avg')

plt.legend()

plt.title('Median Salary (USD) by Age')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')

plt.tight_layout()

plt.show()

Bar[edit]

Vertical[edit]

import numpy as np
from matplotlib import pyplot as plt


ages_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
x_indexes = np.arange(len(ages_x))
width = 0.25

dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.bar(x_indexes - width, dev_y, width=width, color="#444444", label="All Devs")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.bar(x_indexes, py_dev_y, width=width, color="#008fd5", label="Python")

js_dev_y = [37810, 43515, 46823, 49293, 53437,
            56373, 62375, 66674, 68745, 68746, 74583]
plt.bar(x_indexes + width, js_dev_y, width=width, color="#e5ae38", label="JavaScript")

plt.legend()
plt.xticks(ticks=x_indexes, labels=ages_x)
plt.title("Median Salary (USD) by Age")
plt.xlabel("Ages")
plt.ylabel("Median Salary (USD)")

plt.tight_layout()

plt.show()

Horizontal[edit]

import csv
import numpy as np
import pandas as pd
from collections import Counter
from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")

data = pd.read_csv('data.csv')
ids = data['Responder_id']
lang_responses = data['LanguagesWorkedWith']

language_counter = Counter()

for response in lang_responses:
    language_counter.update(response.split(';'))

languages = []
popularity = []

for item in language_counter.most_common(15):
    languages.append(item[0])
    popularity.append(item[1])

languages.reverse()
popularity.reverse()

plt.barh(languages, popularity)

plt.title("Most Popular Languages")
# plt.ylabel("Programming Languages")
plt.xlabel("Number of People Who Use")

plt.tight_layout()

plt.show()

Pie[edit]

from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")

slices = [59219, 55466, 47544, 36443, 35917]
labels = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java']
explode = [0, 0, 0, 0.1, 0]

plt.pie(slices, labels=labels, explode=explode, shadow=True,
        startangle=90, autopct='%1.1f%%',
        wedgeprops={'edgecolor': 'black'})

plt.title("My Awesome Pie Chart")
plt.tight_layout()
plt.show()

Stack[edit]

from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")


minutes = [1, 2, 3, 4, 5, 6, 7, 8, 9]

player1 = [8, 6, 5, 5, 4, 2, 1, 1, 0]
player2 = [0, 1, 2, 2, 2, 4, 4, 4, 4]
player3 = [0, 1, 1, 1, 2, 2, 3, 3, 4]

labels = ['player1', 'player2', 'player3']
colors = ['#6d904f', '#fc4f30', '#008fd5']

plt.stackplot(minutes, player1, player2, player3, labels=labels, colors=colors)

plt.legend(loc=(0.07, 0.05))
# plt.legend(loc="upper left")


plt.title("My Awesome Stack Plot")
plt.tight_layout()
plt.show()

Histogram[edit]

import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('fivethirtyeight')

data = pd.read_csv('data.csv')
ids = data['Responder_id']
ages = data['Age']

bins = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

plt.hist(ages, bins=bins, edgecolor='black', log=True)

median_age = 29
color = '#fc4f30'

plt.axvline(median_age, color=color, label='Age Median', linewidth=2)

plt.legend()

plt.title('Ages of Respondents')
plt.xlabel('Ages')
plt.ylabel('Total Respondents')

plt.tight_layout()

plt.show()

Scatter plot[edit]

import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

data = pd.read_csv('2019-05-31-data.csv')
view_count = data['view_count']
likes = data['likes']
ratio = data['ratio']

plt.scatter(view_count, likes, c=ratio, cmap='summer',
            edgecolor='black', linewidth=1, alpha=0.75)

cbar = plt.colorbar()
cbar.set_label('Like/Dislike Ratio')

plt.xscale('log')
plt.yscale('log')

plt.title('Trending YouTube Videos')
plt.xlabel('View Count')
plt.ylabel('Total Likes')

plt.tight_layout()

plt.show()

Time Series[edit]

import pandas as pd
from datetime import datetime, timedelta
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates

plt.style.use('seaborn')

data = pd.read_csv('data.csv')

data['Date'] = pd.to_datetime(data['Date'])
data.sort_values('Date', inplace=True)

price_date = data['Date']
price_close = data['Close']

plt.plot_date(price_date, price_close, linestyle='solid')

plt.gcf().autofmt_xdate()  # rotate dates

# date_format = mpl_dates.DateFormatter('%b, %d %Y')
# plt.gca().xaxis.set_major_formatter(date_format)

plt.title('Bitcoin Prices')
plt.xlabel('Date')
plt.ylabel('Closing Price')

plt.tight_layout()

plt.show()

Ploting live data[edit]

import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []

index = count()


def animate(i):
    data = pd.read_csv('data.csv')
    x = data['x_value']
    y1 = data['total_1']
    y2 = data['total_2']

    plt.cla()

    plt.plot(x, y1, label='Channel 1')
    plt.plot(x, y2, label='Channel 2')

    plt.legend(loc='upper left')
    plt.tight_layout()


ani = FuncAnimation(plt.gcf(), animate, interval=1000)

plt.tight_layout()
plt.show()
# ----------------------------------------------------------
# Another way to do it without clearing the Axis
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []

plt.plot([], [], label='Channel 1')
plt.plot([], [], label='Channel 2')


def animate(i):
    data = pd.read_csv('data.csv')
    x = data['x_value']
    y1 = data['total_1']
    y2 = data['total_2']

    ax = plt.gca()
    line1, line2 = ax.lines

    line1.set_data(x, y1)
    line2.set_data(x, y2)

    xlim_low, xlim_high = ax.get_xlim()
    ylim_low, ylim_high = ax.get_ylim()

    ax.set_xlim(xlim_low, (x.max() + 5))

    y1max = y1.max()
    y2max = y2.max()
    current_ymax = y1max if (y1max > y2max) else y2max

    y1min = y1.min()
    y2min = y2.min()
    current_ymin = y1min if (y1min < y2min) else y2min

    ax.set_ylim((current_ymin - 5), (current_ymax + 5))


ani = FuncAnimation(plt.gcf(), animate, interval=1000)

plt.legend()
plt.tight_layout()
plt.show()

Subplots[edit]

import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

data = pd.read_csv('data.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']

# On same figure:
# fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True)

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()

ax1.plot(ages, dev_salaries, color='#444444',
         linestyle='--', label='All Devs')

ax2.plot(ages, py_salaries, label='Python')
ax2.plot(ages, js_salaries, label='JavaScript')

ax1.legend()
ax1.set_title('Median Salary (USD) by Age')
ax1.set_ylabel('Median Salary (USD)')

ax2.legend()
ax2.set_xlabel('Ages')
ax2.set_ylabel('Median Salary (USD)')

plt.tight_layout()

plt.show()

fig1.savefig('fig1.png')
fig2.savefig('fig2.png')