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.

1
  • 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 and here. It is also worth noting that a vertical lollipop plot can be created using the matplotlib stem plot. Commented Feb 2, 2021 at 14:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.