如果想要画如下的图形
先画一个饼图
"""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或
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()
关键词: