Python 如何按行绘制饼图?
如果数据如下所示: 一二 123456 98765 456767 45678 12345487654 那么,如何为第一行值(即熊猫中的12345698765)形成饼图 我尝试了互联网上给出的代码: df.T.plot.piesublots=True,figsize=9,3 及Python 如何按行绘制饼图?,python,pandas,pie-chart,Python,Pandas,Pie Chart,如果数据如下所示: 一二 123456 98765 456767 45678 12345487654 那么,如何为第一行值(即熊猫中的12345698765)形成饼图 我尝试了互联网上给出的代码: df.T.plot.piesublots=True,figsize=9,3 及 但这些代码不会绘制行值,而是给出一个cloumn结果 如果要获得逐行饼图,可以迭代行并绘制每行: In [1]: import matplotlib.pyplot as plt ...: import pandas
但这些代码不会绘制行值,而是给出一个cloumn结果 如果要获得逐行饼图,可以迭代行并绘制每行:
In [1]: import matplotlib.pyplot as plt
...: import pandas as pd
...: import seaborn as sns
...:
...: sns.set(palette='Paired')
...: %matplotlib inline
In [2]: df = pd.DataFrame(columns=['one', 'two'], data=[[123456, 98765],[456767, 45678],[123454, 87654]])
In [3]: df.head()
Out[3]:
one two
0 123456 98765
1 456767 45678
2 123454 87654
In [4]: for ind in df.index:
...: fig, ax = plt.subplots(1,1)
...: fig.set_size_inches(5,5)
...: df.iloc[ind].plot(kind='pie', ax=ax, autopct='%1.1f%%')
...: ax.set_ylabel('')
...: ax.set_xlabel('')
...:
<matplotlib.figure.Figure at 0x1e8b4205c50>
<matplotlib.figure.Figure at 0x1e8b41f56d8>
<matplotlib.figure.Figure at 0x1e8b4437438>
请参阅有关的文档,您在哪里卡住了?你能提供一些代码示例吗?或者看看和。我已经更新了我的代码,请看一下主部分@Elletlar谢谢。谢谢@Benjamin它适用于所有行。但是一个饼图怎么能只画一行呢?比如说[0]行?嗨,苏里亚,我已经扩展了我的答案。明白了。。。谢谢@Benjamin
In [1]: import matplotlib.pyplot as plt
...: import pandas as pd
...: import seaborn as sns
...:
...: sns.set(palette='Paired')
...: %matplotlib inline
In [2]: df = pd.DataFrame(columns=['one', 'two'], data=[[123456, 98765],[456767, 45678],[123454, 87654]])
In [3]: df.head()
Out[3]:
one two
0 123456 98765
1 456767 45678
2 123454 87654
In [4]: for ind in df.index:
...: fig, ax = plt.subplots(1,1)
...: fig.set_size_inches(5,5)
...: df.iloc[ind].plot(kind='pie', ax=ax, autopct='%1.1f%%')
...: ax.set_ylabel('')
...: ax.set_xlabel('')
...:
<matplotlib.figure.Figure at 0x1e8b4205c50>
<matplotlib.figure.Figure at 0x1e8b41f56d8>
<matplotlib.figure.Figure at 0x1e8b4437438>
fig, ax = plt.subplots(1,1)
fig.set_size_inches(5,5)
df.iloc[0].plot(kind='pie', ax=ax, autopct='%1.1f%%')
ax.set_ylabel('')
ax.set_xlabel('')