Matplotlib: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(18 intermediate revisions by the same user not shown)
Line 4: Line 4:


=Internal=
=Internal=
* [[Data Science#Visualization|Data Science]]
* [[Python_for_Data_Analysis#Overview|Python for Data Analysis]]


=Overview=
=Overview=
=Plot Types=
 
matplotlib is the most popular Python library for producing plots and other two-dimensional data visualizations.
 
=Install=
<syntaxhighlight lang='bash'>
pip3 install matplotlib
</syntaxhighlight>
=Import Convention=
<syntaxhighlight lang='py'>
import matplotlib.pyplot as plt
</syntaxhighlight>
 
=Visualization Types=
{{External|https://matplotlib.org/stable/plot_types/index.html}}
{{External|https://matplotlib.org/stable/plot_types/index.html}}
==Plot==
==Plot==
{{External|Example https://matplotlib.org/stable/plot_types/basic/plot.html#sphx-glr-plot-types-basic-plot-py}}
{{Internal|matplotlib Plot#Overview|Plot}}


==Scatter==
==Scatter==
{{Internal|matplotlib Scatter|Scatter}}
==Difference between Plot and Scatter==
A "plot" expects two X and Y vectors as argument, which are expected to have the same length, and it creates a 2D line that includes the (X,Y) points. The points are connected by line segments.
=Visualization Style=
The visualization style can be set globally with:
<syntaxhighlight lang='py'>
import matplotlib.pyplot as plt
plt.style.use(<style-name>)
</syntaxhighlight>
where available style names can be obtained with:
<syntaxhighlight lang='py'>
import matplotlib.pyplot as plt
print(plt.style.available)
</syntaxhighlight>
<syntaxhighlight lang='text'>
Solarize_Light2
_classic_test_patch
_mpl-gallery
_mpl-gallery-nogrid
bmh
classic
dark_background
fast
fivethirtyeight
ggplot
grayscale
seaborn-v0_8
seaborn-v0_8-bright
seaborn-v0_8-colorblind
seaborn-v0_8-dark
seaborn-v0_8-dark-palette
seaborn-v0_8-darkgrid
seaborn-v0_8-deep
seaborn-v0_8-muted
seaborn-v0_8-notebook
seaborn-v0_8-paper
seaborn-v0_8-pastel
seaborn-v0_8-poster
seaborn-v0_8-talk
seaborn-v0_8-ticks
seaborn-v0_8-white
seaborn-v0_8-whitegrid
tableau-colorblind10
</syntaxhighlight>
Interesting styles:
* seaborn-v0_8-whitegrid
Otherwise, style elements like axes, gridlines, line color and width can be set up individually.
==Color Palette==
A list of colors that can be used to change the colors in plots, so they don't look the same. This line pulls the default colors from the [[Matplotlib#Visualization_Style|style]].
<syntaxhighlight lang='py'>
color_pal = plt.rcParams["axes.prop_cycle"].by_key()["color"]
</syntaxhighlight>

Latest revision as of 00:02, 15 May 2024

External

Internal

Overview

matplotlib is the most popular Python library for producing plots and other two-dimensional data visualizations.

Install

pip3 install matplotlib

Import Convention

import matplotlib.pyplot as plt

Visualization Types

https://matplotlib.org/stable/plot_types/index.html

Plot

Plot

Scatter

Scatter

Difference between Plot and Scatter

A "plot" expects two X and Y vectors as argument, which are expected to have the same length, and it creates a 2D line that includes the (X,Y) points. The points are connected by line segments.

Visualization Style

The visualization style can be set globally with:

import matplotlib.pyplot as plt 
plt.style.use(<style-name>)

where available style names can be obtained with:

import matplotlib.pyplot as plt 

print(plt.style.available)
Solarize_Light2
_classic_test_patch
_mpl-gallery
_mpl-gallery-nogrid
bmh
classic
dark_background
fast
fivethirtyeight
ggplot
grayscale
seaborn-v0_8
seaborn-v0_8-bright
seaborn-v0_8-colorblind
seaborn-v0_8-dark
seaborn-v0_8-dark-palette
seaborn-v0_8-darkgrid
seaborn-v0_8-deep
seaborn-v0_8-muted
seaborn-v0_8-notebook
seaborn-v0_8-paper
seaborn-v0_8-pastel
seaborn-v0_8-poster
seaborn-v0_8-talk
seaborn-v0_8-ticks
seaborn-v0_8-white
seaborn-v0_8-whitegrid
tableau-colorblind10

Interesting styles:

  • seaborn-v0_8-whitegrid

Otherwise, style elements like axes, gridlines, line color and width can be set up individually.

Color Palette

A list of colors that can be used to change the colors in plots, so they don't look the same. This line pulls the default colors from the style.

color_pal = plt.rcParams["axes.prop_cycle"].by_key()["color"]