焦点热讯:python 100天 94 画一个变化的彩色轮盘

2023-04-09 22:08:04 来源:洪较瘦不着调是么


如果想要画如下的图形


先画一个饼图

"""Created on 2023年4月9日@author: admin"""import numpy as npimport matplotlib.pyplot as pltn = 20X = np.ones(n)X[-1] *= 2plt.pie(X, explode=X*.05, colors = ["%f" % (i/float(n)) for i in range(n)])



【资料图】


继续进行参数设置

fig = plt.gcf()w, h = fig.get_figwidth(), fig.get_figheight()r = h / float(w)plt.xlim(-1.5, 1.5)plt.ylim(-1.5 * r, 1.5 * r)plt.xticks([])plt.yticks([])


画上面的一个框



from matplotlib.patches import FancyBboxPatchax = plt.gca()ax.add_patch(FancyBboxPatch((-0.05, .87),          width=.66, height=.165, clip_on=False,          boxstyle="square,pad=0", zorder=3,          facecolor="white", alpha=1.0,          transform=plt.gca().transAxes))

FancyBboxPatch主要是实现一个框形区域,后面专门讲这个例子

FancyBboxPatch 类似于 Rectangle ,但它在矩形周围画了一个漂亮的方框。矩形框到花式框的转换委托给中定义的样式类 BoxStyle .

参数:

xy浮起,浮起

框的左下角。

width浮动

盒子的宽度。

height浮动

盒子的高度。

箱型STR或 matplotlib.patches.BoxStylestr或

等级

名字

阿特斯

圆圈

circle

pad=0.3

DArrow

darrow

pad=0.3

LArrow

larrow

pad=0.3

RArrow

rarrow

pad=0.3

回合

round

焊盘=0.3,圆角尺寸=无

第四回合

round4

焊盘=0.3,圆角尺寸=无

圆齿

roundtooth

衬垫=0.3,齿尺寸=无

锯齿形

sawtooth

衬垫=0.3,齿尺寸=无

正方形

square

pad=0.3

mutation_scale浮点,默认值:1

应用于填充样式的缩放因子。

mutation_aspect可选浮动

在变异之前,矩形的高度将被该值压缩,并且变异的长方体将被它的倒数拉伸。例如,这允许不同的水平和垂直填充。


添加文字


plt.text(-0.05, 1.02, " Pie Chart: plt.pie(...)\n",          horizontalalignment="left",          verticalalignment="top",          size="xx-large",          transform=plt.gca().transAxes)
plt.text(-0.05, 1.01, "\n\n Make a pie chart of an array ",horizontalalignment="left",verticalalignment="top",size="large",transform=plt.gca().transAxes)

完整的代码如下


"""Created on 2023年4月9日@author: admin"""import numpy as npimport matplotlib.pyplot as pltn = 20X = np.ones(n)X[-1] *= 2plt.pie(X, explode=X*.05, colors = ["%f" % (i/float(n)) for i in range(n)])fig = plt.gcf()w, h = fig.get_figwidth(), fig.get_figheight()r = h / float(w)plt.xlim(-1.5, 1.5)plt.ylim(-1.5 * r, 1.5 * r)plt.xticks([])plt.yticks([])# Add a title and a box around itfrom matplotlib.patches import FancyBboxPatchax = plt.gca()ax.add_patch(FancyBboxPatch((-0.05, .87),      width=.66, height=.165, clip_on=False,      boxstyle="square,pad=0", zorder=3,      facecolor="white", alpha=1.0,      transform=plt.gca().transAxes))#plt.text(-0.05, 1.02, " Pie Chart: plt.pie(...)\n",        horizontalalignment="left",        verticalalignment="top",        size="xx-large",        transform=plt.gca().transAxes)plt.text(-0.05, 1.01, "\n\n Make a pie chart of an array ",        horizontalalignment="left",        verticalalignment="top",        size="large",        transform=plt.gca().transAxes)plt.show()


import numpy as npimport matplotlib.pyplot as pltn = 20X = np.ones(n)X[-1] *= 2plt.pie(X, explode=X*.05, colors = ["%f" % (i/float(n)) for i in range(n)])fig = plt.gcf()w, h = fig.get_figwidth(), fig.get_figheight()r = h / float(w)plt.xlim(-1.5, 1.5)plt.ylim(-1.5 * r, 1.5 * r)plt.xticks([])plt.yticks([])# Add a title and a box around itfrom matplotlib.patches import FancyBboxPatchax = plt.gca()ax.add_patch(FancyBboxPatch((-0.05, .87),                            width=.66, height=.165, clip_on=False,                            boxstyle="square,pad=0", zorder=3,                            facecolor="white", alpha=1.0,                            transform=plt.gca().transAxes))plt.text(-0.05, 1.02, " Pie Chart:           plt.pie(...)\n",        horizontalalignment="left",        verticalalignment="top",        size="xx-large",        transform=plt.gca().transAxes)plt.text(-0.05, 1.01, "\n\n   Make a pie chart of an array ",        horizontalalignment="left",        verticalalignment="top",        size="large",        transform=plt.gca().transAxes)plt.show()

关键词:

上一篇 :

下一篇 :

X 广告
公司
X 广告

Copyright   2015-2022 时代快递网版权所有  备案号:   联系邮箱: 514 676 113@qq.com