2017-11-03 88 views
0

我想学习python主要是为了绘图。下面是我的示例代码:格式化为使用matplotlib的酒吧组

import numpy as np 
import matplotlib.pyplot as plt 


a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]] 
x=np.arange(len(a[0])) 
width=0.2 

fig, ax = plt.subplots(figsize=(8,6)) 
patterns=['/','\\','*'] 

for bar in a: 
    ax.bar(x,bar,width,edgecolor='black',color='lightgray', hatch=patterns.pop(0)) 
    x=x+width 

plt.show() 

现在的问题是,我需要为所有的酒吧边缘颜色以及给予孵化拍打。但是,格式仅适用于第一组条形。这是我的输出。 (我正在使用python3)。

enter image description here

现在缺少的这里或有什么不对?我环顾四周,但没有找到任何修复。

更新: 我已经尝试了不同的选择:python2,python3和pdf/png。这里有结果

  • python2 PNG --fine
  • python3 PNG - 上面
  • python2 PDF显示 - 见this
  • python3 PDF - 见this

我也有尝试'后端'为matplotlib.use('Agg')。我已更新我的matplotlib版本(2.1.0)。

回答

2

Edgecolor元组的alpha值看起来有问题。将其设置为1将解决问题。

+0

正确的,但我有整整三点式的酒吧,也就是第一条 - 前进斜杠,第二杆 - 后面的斜线,最后开始。而不是流行,做patter [我]将返回相同。 – novice

+0

每个酒吧的边框颜色怎么样? – novice

+0

编辑,itertools包可能会有更好的解决方案。 –

2

matplotlib 2.1中有一个current issue只有第一个bar的edgecolor被应用。舱口的相同,请参见this issue。另见this question

这可能是因为你正在使用matplotlib 2.1 for python3而不是python2,因此在python2中它适用于你。如果我用matplotlib 2.1在python 2中运行你的代码,我会得到相同的不需要的行为。

一旦matplotlib 2.1.1发布,问题将被修复。

在此期间,一个解决方法是设置在各个酒吧edgecolor和孵化:

import numpy as np 
import matplotlib.pyplot as plt 


a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]] 
x=np.arange(len(a[0])) 
width=0.2 

fig, ax = plt.subplots(figsize=(8,6)) 
patterns=['/','\\','*'] 

for y in a: 
    bars = ax.bar(x,y,width,color='lightgray') 
    hatch= patterns.pop(0) 
    for bar in bars: 
     bar.set_edgecolor("black") 
     bar.set_hatch(hatch) 
    x=x+width 

plt.show() 

enter image description here