22

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

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

4

3 回答 3

43
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()

在此处输入图像描述

它在此处的文档中。

好的 - 所以要纹理饼图,你需要这样做:

如果你看这里

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()

在此处输入图像描述

于 2013-01-11T13:55:38.597 回答
29

使用bar(),您可以直接使用舱口盖(带有一些后端):http ://matplotlib.org/examples/pylab_examples/hatch_demo.html : 带阴影的条形图

它通过将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()

阴影饼图]

于 2013-01-11T13:48:45.880 回答
0

这可能会帮助您:

http://matplotlib.org/examples/pylab_examples/demo_ribbon_box.html

它使用matplotlib.image.BboxImage

我相信这可以根据输入数据调整给定图像的大小。

于 2017-08-09T15:23:43.330 回答