Comprehensive guide to Matplotlib

=> Simple plot with 2 axis

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]

plt.plot(age_x, dev_y)
plt.show()

=> Simple plot with 2 axis with xlabel, ylabel and title

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]

plt.plot(age_x, dev_y)
plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")

plt.show()

=> Plot multi-dimentional data

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y)

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y)

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.show()

=> Plot multi-dimentional data with legend by giving value in list

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y)

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y)

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend(["All Developers", "Python Developers"])
plt.show()
Limitation: Need to take care of sequence of plots

=> Plot multi-dimentional data with legend by giving parameter in plot method

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()

plt.show()

=> Plot line chart with format string which consists marker, line and color

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, 'k--', label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, 'b', label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()

plt.show()
Markers
characterdescription
'.'point marker
','pixel marker
'o'circle marker
'v'triangle_down marker
'^'triangle_up marker
'<'triangle_left marker
'>'triangle_right marker
'1'tri_down marker
'2'tri_up marker
'3'tri_left marker
'4'tri_right marker
's'square marker
'p'pentagon marker
'*'star marker
'h'hexagon1 marker
'H'hexagon2 marker
'+'plus marker
'x'x marker
'D'diamond marker
'd'thin_diamond marker
'|'vline marker
'_'hline marker
Line Styles
characterdescription
'-'solid line style
'--'dashed line style
'-.'dash-dot line style
':'dotted line style
Colors
The supported color abbreviations are the single letter codes
charactercolor
'b'blue
'g'green
'r'red
'c'cyan
'm'magenta
'y'yellow
'k'black
'w'white

=> Plot a graph using parameters color, marker and linestyle of plot method

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, color='k', linestyle='--', marker='.', label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, color='b', marker='o', label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.show()

=> Plot a line chart using parameter color with hex value

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, color='#FF5733', linestyle='--', marker='.', label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, color='#1CB00B', marker='o', label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()

plt.show()

=> Plot a line chart using parameter linewidth

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, color='#FF5733', linestyle='--', linewidth=3, marker='.', label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, color='#1CB00B', marker='o', label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()

plt.show()
Note: Change the position of plt.plot statements accordingly in case a line overlaps the other line due to width.

=> Plot a line chart with padding and adding grid

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, color='#FF5733', linestyle='--', marker='.', label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, color='#1CB00B', marker='o', label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.grid(True)
plt.tight_layout()

plt.show()
=> Plot a line chart using built-in style

Available styles are,

from matplotlib import pyplot as plt
print(plt.style.available)

['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', '_classic_test']

from matplotlib import pyplot as plt
plt.style.use("fivethirtyeight")
age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()
plt.show()


=> Plot a line chart in a cartoonic way - Use xkcd() method

from matplotlib import pyplot as plt

plt.xkcd()

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()

plt.show()
=> Save chart

from matplotlib import pyplot as plt

plt.xkcd()

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(age_x, dev_y, label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, label="Python Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()
plt.savefig("myChart.png")
plt.show()

=> Plot a simple bar chart

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.bar(age_x, dev_y)

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()
plt.show()
=> Plot a bar chart with line charts for different lists to comparision

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.bar(age_x, dev_y, label="All Developers")

py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
plt.plot(age_x, py_dev_y, label="Python Developers")

js_dev_y = [37810, 43515, 46823, 49293, 53437,
            56373, 62375, 66674, 68745, 68746, 74583]
plt.plot(age_x, js_dev_y, label="JavaScript Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()
plt.show()
Note: Issue with draw the bar chart for all, may overlap to other bars. In below example All developer's bars are hidden.

from matplotlib import pyplot as plt

age_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
plt.bar(age_x, dev_y, color="#444444", label="All Developers")

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

js_dev_y = [37810, 43515, 46823, 49293, 53437,
            56373, 62375, 66674, 68745, 68746, 74583]
plt.bar(age_x, js_dev_y, color="#E5AE38", label="JavaScript Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()
plt.show()
=> Plot a bar chart side by side for different lists to comparision


import numpy as np
from matplotlib import pyplot as plt

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

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 Developers")

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 Developers")

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 Developers")

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.tight_layout()
plt.show()
Replace x-axis indexes to age_x

import numpy as np
from matplotlib import pyplot as plt

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

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 Developers")

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 Developers")

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 Developers") 

plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Dev Salary (USD) by Age")
plt.legend()
plt.xticks(x_indexes, age_x)
plt.tight_layout()
plt.show()

Comments

Popular posts from this blog

DVB Q&A

Solved - OSError("(10060, 'WSAETIMEDOUT')" in Requests package in Python