gpt4 book ai didi

python - 如何为我的杆和楔形添加纹理?

转载 作者:太空狗 更新时间:2023-10-29 17:52:02 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于python - 如何为我的杆和楔形添加纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14279344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com