2013-01-11 77 views
9

我使用matplotlib.pyplot.bar()matplotlib.pyplot.pie()來繪製幾條和餅圖。在這兩種功能中,我都可以更改條形和楔形的顏色。如何添加紋理到我的酒吧和楔子?

但是,我需要用黑白打印這些圖表。能夠在條和楔上放置紋理會更有用,類似於可用於繪製線的標記屬性Line2D。我是否可以用一致的方式用這些標記填充條形和楔形?或者有沒有其他辦法可以實現這樣的目標?

回答

16

隨着bar(),您可以直接使用艙口(一些後端):http://matplotlib.org/examples/pylab_examples/hatch_demo.htmlbar plot with hatches

它通過添加hatch參數您的來電bar()


至於pie(),它沒有一個hatch關鍵字。你可以代替獲取單個餅圖補丁並添加艙口對他們說:你的補丁有:

patches = pie(…)[0] # The first element of the returned tuple are the pie slices 

那麼你申請的艙門,以每片(補丁):

patches[0].set_hatch('/') # Pie slice #0 hatched. 

(艙口名單在http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch.set_hatch)。

並應用與變化:

pyplot.draw() 

Hatched pie chart]

+1

太棒了!我從來沒有發現,如此之快,我自己。非常感謝! :) – pemistahl

19
import matplotlib.pyplot as plt 

fig = plt.figure() 

patterns = [ "/" , "\\" , "|" , "-" , "+" , "x", "o", "O", ".", "*" ] 

ax1 = fig.add_subplot(111) 
for i in range(len(patterns)): 
    ax1.bar(i, 3, color='red', edgecolor='black', hatch=patterns[i]) 


plt.show() 

enter image description here

它的文檔在這裏:

http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch.set_hatch

好了 - 所以紋理一個餅圖,你需要這樣做:

如果你看看here

Return value: 
If autopct is None, return the tuple (patches, texts): 

patches is a sequence of matplotlib.patches.Wedge instances 
texts is a list of the label matplotlib.text.Text instances. 

所以接下來我們看看Wedges頁面,看到它有一個set_hatch( ) 方法。

所以我們只需要添加幾行到餅圖演示和...

實施例1:

import matplotlib.pyplot as plt 

fig = plt.figure() 

patterns = [ "/" , "\\" , "|" , "-" , "+" , "x", "o", "O", ".", "*" ] 

ax1 = fig.add_subplot(111) 
for i in range(len(patterns)): 
    ax1.bar(i, 3, color='red', edgecolor='black', hatch=patterns[i]) 


plt.show() 

實施例2:

""" 
Make a pie chart - see 
http://matplotlib.sf.net/matplotlib.pylab.html#-pie for the docstring. 

This example shows a basic pie chart with labels optional features, 
like autolabeling the percentage, offsetting a slice with "explode", 
adding a shadow, and changing the starting angle. 

""" 

from pylab import * 
import math 
import numpy as np 

patterns = [ "/" , "\\" , "|" , "-" , "+" , "x", "o", "O", ".", "*" ] 


def little_pie(breakdown,location,size): 
    breakdown = [0] + list(np.cumsum(breakdown)* 1.0/sum(breakdown)) 
    for i in xrange(len(breakdown)-1): 
     x = [0] + np.cos(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi *  
          breakdown[i+1], 20)).tolist() 
     y = [0] + np.sin(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi * 
          breakdown[i+1], 20)).tolist() 
     xy = zip(x,y) 
     scatter(location[0], location[1], marker=(xy,0), s=size, facecolor= 
       ['gold','yellow', 'orange', 'red','purple','indigo','violet'][i%7]) 

figure(1, figsize=(6,6)) 

little_pie([10,3,7],(1,1),600) 
little_pie([10,27,4,8,4,5,6,17,33],(-1,1),800) 

fracs = [10, 8, 7, 10] 
explode=(0, 0, 0.1, 0) 

piechart = pie(fracs, explode=explode, autopct='%1.1f%%') 
for i in range(len(piechart[0])): 
    piechart[0][i].set_hatch(patterns[(i)%len(patterns)]) 


show() 

enter image description here

+0

謝謝。 :)我該如何做餡餅楔子? – pemistahl

+0

@PeterStahl - 對不起,我從來沒有使用餅圖。 set_hatch是藝術家的一種方法,所以你應該能夠設置它獲得每個片的藝術家。 – will

+0

@PeterStahl好吧,工作的方式來做到這一點。 – will