A comprehensive guide for using python plotting library matplotlib which covers most of the necessary plots for data visualization.
A Beginner's Guide to Matplotlib
Matplotlib
importmatplotlib.pyplotaspltfig, ax=plt.subplots()
ax.plot(x,y)
plt.show()
# adding a marker to the plot'# visit https://matplotlib.org/stable/api/markers_api.html'ax.plot(x, y, marker="o")
# changing the linestyleax.plot(x, y, marker="o", linestyle="--")
# visit: https://matplotlib.org/stable/gallery/lines_bars_and_markers/linestyles.html# changing the line colorax.plot(x, y, marker="o", linestyle="--", color='r')
# setting the plot labelsax.set_xlabel("This is x label")
ax.set_ylabel("This is y label")
ax.set_title("This is the title")
# small multiplesfig, ax=plt.subplots(3,2)
# this will return an array of subplots which can be indexed for plotting# this will insure the range on y-axis is same in all subplotsfig, ax=plt.subplots(3,2)
Plotting Time Series Data
fig, ax=plt.subplots()
ax.plot(df.index, df['col'])
ax.set_xlabel("This is x-label")
ax.set_ylabel("This is y-label")
# Using different y-axisax2=ax.twinx()
ax2.plot(df.index, df['col_1']) # different y-axis but same x-axisax2.set_ylabel("This is y-label for 2nd plot")
plt.show()
# we can give color to the plots as well as the labels and ticksax.tick_params('either x or y', color='color_name')
Adding Annotations
ax.annotate("annotation text", xy=(pd.TimeStamp("yyyy-mm-dd"), 1))
# moving the annotation:ax.annotate("annotation text", xy=(pd.TimeStamp("yyyy-mm-dd"), 1), xytext=(pd.Timestamp("yyyy-mm-dd"), -0.2))
# adding arrow to connect the annotation and plot pointax2.annotate(">1 degree", xy=(pd.Timestamp("yyyy-mm-dd"), 1),
xytext=(pd.Timestamp("yyyy-mm-dd"), -0.2),
arrowprops={"arrowstyle": "->", "color":"gray"})
Bar Charts
# simple bar chatfig, ax=plt.subplots()
ax.bar(x, y)
plt.show()
# rotating the x-tick labels by 90 degreeax.set_xticklabels(column, rotation=90)
# creating a stacked bar-chartax.bar(x1, y)
ax.bar(x2, y, bottom=x1)
ax.bar(x3, y, bottom=x1+x2)
# adding legendsax.legend()
# showing plotax.show()
Histograms
fig, ax=plt.sublplots()
ax.hist(dataframe['column'])
plt.show()
ax.hist(dataframe['column'], label="label for this histogram", bins="number of bins")
# we can also provide a list of bins in bins parameterax.hist(dataframe['column'], bins=[20, 40, 60, 80, 100])
# sometimes if we plot multiple variables in a single plot our plot can be# overlapping so we can change the histogram type so it can be hollow and# visibleax.hist(dataframe['column'], bins=[20, 40, 60, 80, 100], histtype="Step")
fig, ax=plt.subplots()
ax.scatter(climate_change['co2'], climate_change['relative_temp'])
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (C)")
plt.show()
# giving time index to scatter plot as c paramfig, ax=plt.subplots()
ax.scatter(climate_change['co2'], climate_change['relative_temp'], c=climate_change.index)
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (C)")
plt.show()