2017-11-03 89 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