How to create lollipop charts from a pandas dataframe
Read in your data
street | shootings | duration | dur_per_day | dur_per_hour | |
---|---|---|---|---|---|
0 | Java Str | 161 | 2547.0 | 1.163014 | 0.048459 |
1 | East 18 Str | 170 | 2547.0 | 1.163014 | 0.048459 |
2 | Grand Str | 168 | 2619.0 | 1.195890 | 0.049829 |
3 | Eagle Str | 180 | 2626.0 | 1.199087 | 0.049962 |
4 | 10 Av | 159 | 2638.0 | 1.204566 | 0.050190 |
The inflexible way: using plt.stem
After I wrote this rest of this, I read about plt.stem
. It makes everything so easy! But it’s
kiiiind of inflexible, which is why you might want to try the other options.
(0, 1.5)
Building a horizontal lollipop graph with a categorical y axis
We’re going to use plot.subplots()
to make an empty graph, then…
ax.hlines
is going to draw horizontal lines. This takes anxmin
and anxmax
.ax.plot
is going to draw circles (like a scatterplot)ax.set_xlim
to make the graph start at0
Not so bad!
(0, 1.5)
Building a verticla lollipop graph with a categorical x axis
It’s basically the same as before, but all of the horizontal lines become
vertical, and our axes are switched on our scatterplot. We’re going to use
plot.subplots()
to make an empty graph, then…
ax.vlines
is going to draw vertical lines. This takes aymin
and aymax
.ax.plot
is going to draw circles (like a scatterplot)ax.set_ylim
to make the graph start at0
Just as easy!
(0, 1.5)