【问题标题】:Plot x-ticks in histogram matplotlib在直方图 matplotlib 中绘制 x 刻度
【发布时间】:2013-11-13 19:52:23
【问题描述】:

我想为给定名称绘制相应的 x。 我的意思是,对于foo,它必须以直方图的形式绘制[10,20,30],并且所有foo、bar、baz都需要在同一个graph中。(我不需要3d :))

import pylab as P
name = ['foo', 'bar', 'baz']
x  = [[10,20,30],[40,50,60],[70,80,90]]

P.figure()
P.hist(x, 10,  histtype='bar',
                color=['crimson', 'burlywood', 'chartreuse'],
                label=['Crimson', 'Burlywood', 'Chartreuse'])

P.show()

【问题讨论】:

  • 请更好地解释您需要什么。将 [10,20,30] 绘制为直方图是什么意思?
  • 你能帮我画出附图中的图表吗?我试过了,但失败了。

标签: python matplotlib plot histogram


【解决方案1】:

希望对您有所帮助:

from matplotlib import pyplot as plt
import numpy as np

names = ['foo', 'bar', 'baz']
x  = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
colors = ['crimson', 'burlywood', 'chartreuse']

y = zip(*x)
groups = len(x)
members = len(y)
pos = np.arange(groups)
width = 1. / (1 + members)

fig, ax = plt.subplots()    
for idx, (serie, color) in enumerate(zip(y, colors)):
    ax.bar(pos + idx * width, serie, width, color=color)

ax.set_xticks(pos + width)
ax.set_xticklabels(names)

plt.show()

【讨论】:

  • 我建议ax.set_xticks(pos + 1.5*width) 将标签集中在组下
  • @spinup 对了,谢谢,我们可以让 OP 优化视图并了解正在做什么......
  • 我明白了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
相关资源
最近更新 更多