如何给我的条和楔形物体添加纹理?

2024-09-30 14:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用^{}^{}绘制几个条形图和饼图。在这两个函数中,我都可以更改条形图和楔形图的颜色。

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


Tags: 方法函数标记目的属性颜色方式图表
3条回答

这可能有助于您:

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

它使用matplotlib.image.BboxImage

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

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()方法。

所以我们只需要在piechart演示中添加几行。。。

例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

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

相关问题 更多 >