8000 categorical variable example · matplotlib/matplotlib@aa88fae · GitHub
[go: up one dir, main page]

Skip to content

Commit aa88fae

Browse files
committed
categorical variable example
1 parent edf2cf8 commit aa88fae

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

doc/api/pyplot_summary.rst

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
Plotting commands summary
2-
=========================
1+
Below we describe several common approaches to plotting with Matplotlib.
2+
3+
.. contents::
4+
5+
The Pyplot API
6+
--------------
37

48
The :mod:`matplotlib.pyplot` module contains functions that allow you to generate
59
many kinds of plots quickly. For examples that showcase the use
610
of the :mod:`matplotlib.pyplot` module, see the
711
:ref:`sphx_glr_tutorials_01_introductory_pyplot.py`
8-
or the :ref:`pyplots_examples`.
12+
or the :ref:`pyplots_examples`. We also recommend that you look into
13+
the object-oriented approach to plotting, described below.
14+
15+
.. currentmodule:: matplotlib.pyplot
16+
17+
.. autofunction:: plotting
918

1019
The Object-Oriented API
1120
-----------------------
21+
1222
Most of these functions also exist as methods in the
1323
:class:`matplotlib.axes.Axes` class. You can use them with the
1424
so-called "Object Oriented" approach to Matplotlib.
@@ -19,11 +29,13 @@ we recommend using the object-oriented approach for more control
1929
and customization of your plots. For some examples of the OO approach
2030
to Matplotlib, see the :ref:`api_examples` examples.
2131

22-
The Pyplot API
23-
--------------
32+
Colors in Matplotlib
33+
--------------------
2434

25-
.. currentmodule:: matplotlib.pyplot
35+
There are many colormaps you can use to map data onto color values.
36+
Below we list several ways in which color can be utilized in Matplotlib.
2637

27-
.. autofunction:: plotting
38+
For a more in-depth look at colormaps, see the
39+
:ref:`sphx_glr_tutorials_colors_colormaps.py` tutorial.
2840

2941
.. autofunction:: colormaps
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
==============================
3+
Plotting categorical variables
4+
==============================
5+
6+
How to use categorical variables in matplotlib.
7+
8+
Many times you want to create a plot that uses categorical variables
9+
in Matplotlib. For example, your data may naturally fall into
10+
several "bins" and you're interested in summarizing the data per
11+
bin. Matplotlib allows you to pass categorical variables directly to
12+
many plotting functions.
13+
"""
14+
import matplotlib.pyplot as plt
15+
16+
names = ['group_a', 'group_b', 'group_c']
17+
values = [1, 10, 100]
18+
19+
fig, axs = plt.subplots(1, 3, figsize=(9, 3))
20+
axs[0].bar(names, values)
21+
axs[1].scatter(names, values)
22+
axs[2].plot(names, values)
23+
fig.suptitle('Categorical Plotting')
24+
25+
plt.show()

tutorials/01_introductory/pyplot.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@
118118
plt.ylabel('entry b')
119119
plt.show()
120120

121+
###############################################################################
122+
# .. _plotting-with-categorical-vars:
123+
#
124+
# Plotting with categorical variables
125+
# ===================================
126+
#
127+
# It is also possible to create a plot using categorical variables
128+
# Matplotlib allows you to pass categorical variables directly to
129+
# many plotting functions. For example:
130+
131+
names = ['group_a', 'group_b', 'group_c']
132+
values = [1, 10, 100]
133+
134+
fig, axs = plt.subplots(1, 3, figsize=(9, 3))
135+
axs[0].bar(names, values)
136+
axs[1].scatter(names, values)
137+
axs[2].plot(names, values)
138+
fig.suptitle('Categorical Plotting')
139+
plt.show()
140+
121141
###############################################################################
122142
# .. _controlling-line-properties:
123143
#

0 commit comments

Comments
 (0)
0