Python 如何为条形和楔形添加纹理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14279344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:58:00  来源:igfitidea点击:

How can I add textures to my bars and wedges?

pythonmatplotlib

提问by pemistahl

I'm drawing several bar and pie charts using matplotlib.pyplot.bar()and matplotlib.pyplot.pie(). In both functions, I can change the colors of the bars and wedges.

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

However, I need to print these charts in black and white. It would be much more useful to be able to put textures on the bars and wedges, similar to the Line2Dmarker property which is available for drawing lines. Can I maybe fill the bars and wedges with these markers in a consistent way? Or is there any other way to achieve something like that?

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

采纳答案by Eric O Lebigot

With bar(), you can directly use hatches (with some backends): http://matplotlib.org/examples/pylab_examples/hatch_demo.html: bar plot with hatches

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

It works by adding the hatchargument to your call to bar().

它的工作原理是将hatch参数添加到您对bar().



As for pie(), it does not have a hatchkeyword. You can instead get the individual pie chart patches and add hatches to them: you get the patches with:

至于pie(),它没有hatch关键字。您可以改为获取单个饼图补丁并为其添加阴影:您可以通过以下方式获得补丁:

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

then you apply the hatches to each slice (patch):

然后将阴影应用于每个切片(补丁):

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

(hatches list at https://matplotlib.org/api/_as_gen/matplotlib.patches.Patch.html#matplotlib.patches.Patch.set_hatch).

(孵化列表在https://matplotlib.org/api/_as_gen/matplotlib.patches.Patch.html#matplotlib.patches.Patch.set_hatch)。

And you apply the changes with:

然后您应用更改:

pyplot.draw()

Hatched pie chart]

孵化饼图]

回答by will

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

在此处输入图片说明

It's in the documentation here.

它在此处的文档中。

Okay - so to texture a piechart, you need to do this:

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

if you look 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.

so then we look at the Wedgespage, and see that it has a set_hatch() method.

所以接下来我们看一下挖起杆网页,看看它有一个set_hatch()方法。

so we just need to add a few lines to the piechart demo and...

所以我们只需要在饼图演示中添加几行,然后......

Example 1:

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

Example 2:

示例 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

在此处输入图片说明

回答by Moustache

This may help you:

这可能会帮助您:

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

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

which uses matplotlib.image.BboxImage

它使用matplotlib.image.BboxImage

I believe this can resize a given image according to input data.

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