Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting bar graph to dot plot in matplotlib?

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 Answers

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

like image 155
joaquin Avatar answered Nov 21 '25 18:11

joaquin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!