Heim  >  Artikel  >  Backend-Entwicklung  >  Beispiel dafür, wie Python die Matplotlib-Bibliothek zum Zeichnen eines Kreisdiagramms verwendet

Beispiel dafür, wie Python die Matplotlib-Bibliothek zum Zeichnen eines Kreisdiagramms verwendet

高洛峰
高洛峰Original
2017-02-10 09:37:432841Durchsuche

Python强大的原因之一就在于其开源,有很多优秀的程序员为其提供了丰富的类库。Matplotlib就是其中之一,下面这篇文章主要介绍了python如何利用matplotlib库绘制饼图的方法示例,有需要的朋友们可以参考借鉴,下面来一起看看吧。

介绍

matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。

它的文档相当完备,并且 Gallery页面 中有上百幅缩略图,打开之后都有源程序。因此如果你需要绘制某种类型的图,只需要在这个页面中浏览/复制/粘贴一下,基本上都能搞定。

这篇文章给大家主要介绍了python用matplotlib绘制饼图的方法,话不多说,下面来看代码。

示例代码


import matplotlib.pyplot as plt

# The slices will be ordered and plotted counter-clockwise.
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

plt.pie(sizes, explode=explode, labels=labels, colors=colors,
  autopct='%1.1f%%', shadow=True, startangle=90)

# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')


plt.savefig('D:\\pie.png')
plt.show()


结果

更多python利用matplotlib库绘制饼图的方法示例相关文章请关注PHP中文网!


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn