6.6 lollipop plot#

# sphinx_gallery_thumbnail_number = -1

import matplotlib.pyplot as plt
import numpy as np
from easy_mpl import lollipop_plot
from easy_mpl.utils import version_info

version_info()
{'easy_mpl': '0.21.4', 'matplotlib': '3.8.2', 'numpy': '1.26.3', 'pandas': '1.5.3', 'scipy': '1.11.4'}

To draw a lollipop we need an array or a list of numeric values

y = np.random.randint(1, 10, size=10)

_ = lollipop_plot(y, title="vanilla")
lollipop

We can also specify the x coordinates for our data as second argument

_ = lollipop_plot(y, np.linspace(0, 100, len(y)), title="with x and y")
lollipop

line style can be set using line_style argument.

_ = lollipop_plot(y, line_style='--', title="with custom linestyle")
lollipop

Similarly marker style can be set using marker_style argument.

_ = lollipop_plot(y, marker_style='D',
                  marker_kws=dict(edgecolor="orange", linewidth=2))
lollipop

the line color can also be a matplotlib colormap name

_ = lollipop_plot(y, line_color="RdBu")
lollipop

We can sort the lollipops by setting the sort to True

_ = lollipop_plot(y, sort=True, title="sort")
lollipop

The orientation of lollipops can be made horizontal

y = np.random.randint(0, 20, size=10)
_ = lollipop_plot(y, orientation="horizontal", title="horizontal")
lollipop

The lollipop plot returns matplotlib axes object which can be used for further manipulation of axes.

y = np.random.randint(-10, 10, 20)
y[y==0] = 1
ax = lollipop_plot(y, marker_color="#D7BFA6",
                   line_color="burlywood",
                   show=False)
ax.axhline(0.0, lw=2.0, color='maroon')
ax.axis('off')
plt.show()
lollipop

Total running time of the script: ( 0 minutes 1.729 seconds)

Gallery generated by Sphinx-Gallery