【问题标题】:converting bar graph to dot plot in matplotlib?在matplotlib中将条形图转换为点图?
【发布时间】:2012-01-29 18:39:47
【问题描述】:

给定以下水平条形图:

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") 

如何改变它,而不是条形,只有顶部带有标记(例如'o')的虚线?就像在点图中一样。谢谢。

【问题讨论】:

  • 我相信您正在寻找的内容通常被称为棒棒糖情节。对于希望在 matplotlib 中创建点图的任何人,您可以找到答案 herehere。还值得注意的是,可以使用matplotlib stem plot 创建垂直棒棒糖图。

标签: python matplotlib


【解决方案1】:

不确定您的问题。你只需要改变:

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

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

在 OP 澄清后编辑:如果你想要水平线,请使用:

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

【讨论】:

  • 谢谢,但我想成为条形而不是曲线,因此虚线应从 x = 0 水平延伸到沿 x 的条形值,不应垂直绘制
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多