Import
import matplotlib.pyplot as plt |
Main principle
# Example
fig, ax = plt.subplots() # Create one subplot
ax.plot(x1, y1) # each ax represents one plot on the figure object
ax.plot(x2, y2) # another plot is added to the figure object (with another color)
plt.show()
Cutomizing data appearance
ax.plot(x,y, marker = "") |
# indicate each data point on the line |
|
ax.plot(x,y, linestyle = "") |
# change linestyle |
|
ax.plot(x,y, color = "") |
# change color |
|
ax.set_xlabel("") |
# name the x axis |
ax.set_ylabel("") |
# name the y axis |
ax.set_title("") |
# give figure a title |
ax.tick_params("x"/"y", colors = "") |
# change the color of the ticks |
ax.set_xticks(list()) |
# change the tick values of the X axis |
Use list comprehension |
ax.set_yticks(list()) |
# change the tick values of the y axis |
Use list comprehension |
scatterplot
fig, ax = plt.scatter(x, y, s=area, c=colors, alpha=0.5)
ax.plot(x, y, linestyle = "None")
plt.show()
|
Subplot
Create the subplots
fig, ax = plt.subplots(3, 2)
First Subplot
ax[0, 0].plot(x, y, color = "green")
Second Subplot
ax[0, 1].plot(x, y, color = "blue")
Subplot
fig, ax = plt.subplots(rows, columns, index_of_subplot) |
fig = container holding one to more axes; ax = individual plot |
ax.plot([x], y, color = "green") |
[X] = X coordinates; Y = Y coordinates |
plt.show() |
Plot the figure. |
plt.subplots(rows, columns, sharey = True) |
All subplots have same range of Y-axis values |
Time series
Once there is column with a DateTime datatype then matplotlib will recognize that this is a variable that represents time. |
Time series
Plot time series |
ax.plot(time_variable, variableOfInterest) |
slice time variable |
timeVariable["start-date":"end-date"] |
Time-series with different variables
fix, ax = plt.subplots()
ax.plot(time_variable, variable1, color = "blue")
ax.set_xlabel()
ax.set_ylabel()
ax2 = ax.twinx() # same x-axis but different y-axis
ax2.plot(time_variable, variable2, color = "red")
ax2.set_xlabel()
ax2.set_ylabel()
plt.show()
|
Add annotations
# First argument is the text of the annotation
# xy argument is the xy of the dataset to which the annotation has to refer (pandas object)
# xytext argument is the xy coordinate of the text
# arrowprops a dictionary that defines the properties of the arrow we would like to use
ax.annotate("", xy = , xytext = , arrowprops = {})
# the arguments for the arrowprops
https://matplotlib.org/tutorials/text/annotations.html
|
Annotations are small pieces of text refering to a particular part of the graph.
Bar chart
fig, ax = plt.subplots()
ax.bar(x-axis, y-axis)
plt.show()
|
barchart
ax.set_xticklabels(names) |
change the names on the x axis |
ax.set_ylabel("") |
Change name of the y axis |
ax.bar(x,y, bottom = , label = "")) |
Important that you use the same x values, set bottom= equal to the pd.series containing the data whereon you want to stack. |
ax.legend() |
ax.set_xticklabels(x, rotation = 90) |
If you want to turn the x labels 90 degrees. |
You can keep adding bars to eachother by calling ax.bar(bottom) multiple times.However, you have to add the different series in bottom:
"medals["Gold"] + medals["Silver"]"
Histogram
fix, ax = plt.subplots()
ax.hist(x, label = "", bins = value/list, histtype = , label = "")
ax.set_xlabel("")
ax.set_ylabel("")
|
Boxplot
fig, ax = plt.subplots()
ax.boxplot(x)
ax.set_ylabel()
# Create multiple boxplots
fig, ax = plt.subplots()
ax.boxplot([mens_rowing["Height"], mens_gymnastics["Height"]])
ax.set_xticklabels(["Rowing", "Gymnastics"])
|
Scatterplot
fix, ax = plt.subplots()
ax.scatter(x, y, color = "" , label = "")
# create mutiple scatters on the same plot with different colors
fix, ax = plt.subplots()
ax.scatter(x, y, color = "red", label ="")
ax.scatter(x, y, color = "blue", label = "")
ax.legend()
ax.set_xlabel()
ax.set_ylabel()
# encoding a third variable by color
fig, ax = plt.subplots()
ax.scatter(x, y, c = )
|
Sharing your visualizations
# save figure
fig.savefig("name.png")
|
|