3

Given the following horizontal bar graph:

import matplotlib.pyplot as plt
from numpy import *
from scipy import *
bars = arange(5) + 0.1
vals = rand(5)
print bars, vals
plt.figure(figsize=(5,5), dpi=100)
spines = ["bottom"]
ax = plt.subplot(1, 1, 1)
for loc, spine in ax.spines.iteritems():
  if loc not in spines:
    spine.set_color('none')
# don't draw ytick marks
#ax.yaxis.set_tick_params(size=0)
ax.xaxis.set_ticks_position('bottom')
plt.barh(bars, vals, align="center")
plt.savefig("test.png") 

how can it be changed so that instead of bars, there are just dotted lines with a marker (e.g. 'o') on top? as in dot plots. thanks.

  • I believe that what you are looking for is more commonly known as a lollipop plot. For anyone looking to create a dot plot in matplotlib, you can find answers [here](https://stackoverflow.com/questions/49703938/how-to-create-a-dot-plot-in-matplotlib-not-a-scatter-plot) and [here](https://stackoverflow.com/questions/57830130/how-do-i-convert-this-histogram-into-a-dot-plot-dot-chart-using-matplotlib-and-n). It is also worth noting that a vertical lollipop plot can be created using the [matplotlib stem plot](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.stem.html). – Patrick FitzGerald Feb 02 '21 at 14:14

1 Answers1

6

Not sure about your question. You only need to change:

plt.barh(bars, vals, align="center")

with

plt.plot(vals, bars, 'o--')

Edited after OP clarification: If you want horizontal lines, use:

plt.plot(vals, bars, 'o')
plt.hlines(bars, [0], vals, linestyles='dotted', lw=2)

enter image description here

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • thanks, but I'd like to be a bar not a curve, so the dotted line should extend horizontally from x = 0 to the value of the bar along x, it should not be plotted vertically –  Jan 29 '12 at 20:17