Pandas Pie 如何绘制椭圆?
Pandas Pie 如何绘制椭圆?
Pandas 提供了画饼图的方法DataFrame.plot.pie()
或 Series.plot.pie()
。
但是发现了已经有趣的事:在有些版本的Pandas中饼图显示的是椭圆图,而我当前版本饼图直接是正圆图。
打开查看互联网发现,在某些版本中Pie确实画椭圆,可以通过plot.pie().axis("equal")
绘制正圆。
哪如何绘制椭圆或者自定义椭圆呢?
在Pandas的源码中可以发现其绘图是通过matplotlib
实现的。
def pie(self, **kwds):
"""
Pie chart
.. versionadded:: 0.17.0
Parameters
----------
**kwds : optional
Keyword arguments to pass on to :py:meth:`pandas.Series.plot`.
Returns
-------
axes : matplotlib.AxesSubplot or np.array of them
"""
return self(kind="pie", **kwds)
那么matplotlib是如何绘制饼图的呢?
官方文档中:matplotlib.pyplot.pie
:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html?highlight=pie#matplotlib.pyplot.pie
其中提到
> The axes aspect ratio can be controlled with Axes.set_aspect.
于是在matplotlib.axes.Axes.set_aspect
中找到设置方法:
可以在pie().set_aspect(aspect=n)
,通过修改圆的长宽比n倍来自定义椭圆。
终于可以快乐的画椭圆了!