如何在条形和楔形中添加纹理?

时间:2013-01-11 13:37:41

标签: python matplotlib

我使用matplotlib.pyplot.bar()matplotlib.pyplot.pie()绘制了几个条形图和饼图。在这两个功能中,我可以更改条形和楔形的颜色。

但是,我需要以黑白打印这些图表。能够在条形和楔形上放置纹理会更有用,类似于可用于绘制线条的Line2D标记属性。我可以用一致的方式用这些标记填充条形和楔形吗?或者还有其他方法来实现这样的目标吗?

2 个答案:

答案 0 :(得分:26)

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

它在文档here中。

好的 - 所以要设置饼图,你需要这样做:

如果你看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

答案 1 :(得分:19)

使用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.

https://matplotlib.org/api/_as_gen/matplotlib.patches.Patch.html#matplotlib.patches.Patch.set_hatch处的阴影列表)。

然后将更改应用于:

pyplot.draw()

Hatched pie chart]