Matplotlib: Difference between revisions

From David's Wiki
Created page with "Matplotlib is used for making plots in Python"
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
Matplotlib is used for making plots in Python
Matplotlib is used for making plots in Python
==Installation==
<pre>
pip install matplotlib
</pre>
==Usage==
Usually you import as follows
<syntaxhighlight lang="bash">
import matplotlib.pyplot as plt
</syntaxhighlight>
===Set size===
<syntaxhighlight lang="python">
f, ax = plt.subplots(figsize=(5, 3))
# or
plt.figure(figsize=(5, 3))
plt.tight_layout()
</syntaxhighlight>
===Saving a plot===
<syntaxhighlight lang="python">
plt.savefig("my_figure.png", dpi=450)
</syntaxhighlight>
===Fonts===
[https://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot Reference]<br>
<syntaxhighlight lang="python">
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE)    # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
</syntaxhighlight>
===Multiple Figures===
<syntaxhighlight lang="python">
f, axarr = plt.subplots(3,1)
axarr[0].imshow(np.clip(x_val[0, 0, :, :, :].numpy(), 0, 1))
axarr[1].imshow(np.clip(val_image[0, :, :, :].numpy(), 0, 1))
axarr[2].imshow(np.clip(y_val[0, :, :, :].numpy(), 0, 1))
</syntaxhighlight>
==Animations==
* [https://towardsdatascience.com/animations-with-matplotlib-d96375c5442c Animations with Matplotlib]

Latest revision as of 19:03, 8 February 2024

Matplotlib is used for making plots in Python

Installation

pip install matplotlib

Usage

Usually you import as follows

import matplotlib.pyplot as plt

Set size

f, ax = plt.subplots(figsize=(5, 3))
# or
plt.figure(figsize=(5, 3))
plt.tight_layout()

Saving a plot

plt.savefig("my_figure.png", dpi=450)

Fonts

Reference

SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12

plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

Multiple Figures

f, axarr = plt.subplots(3,1)
axarr[0].imshow(np.clip(x_val[0, 0, :, :, :].numpy(), 0, 1))
axarr[1].imshow(np.clip(val_image[0, :, :, :].numpy(), 0, 1))
axarr[2].imshow(np.clip(y_val[0, :, :, :].numpy(), 0, 1))

Animations