如何绘制饼图行明智?

时间:2018-07-25 07:27:33

标签: python pandas pie-chart

如果数据如下:

  

一二
  123456 98765
  456767 45678
  123454 87654

那么如何为第一行值(即熊猫中的值12345698765)形成饼图?

我尝试了在互联网上给出的代码:

df.T.plot.pie(subplots=True, figsize=(9, 3))

import matplotlib.pyplot as plt

df.Data.plot(kind='pie')

fig = plt.figure(figsize=(6,6), dpi=200)
ax = plt.subplot(111)

df.Data.plot(kind='pie', ax=ax, autopct='%1.1f%%', startangle=270, fontsize=17)

,但是这些代码不绘制行值,而是给出整块结果。

1 个答案:

答案 0 :(得分:2)

如果要获取逐行饼图,则可以迭代行并绘制每一行:

public class Something 
{
    public int SequenceNumber { get; set; }
}

[Test]
public void SomethingSequenceNumberTest()
{
    var lotsOfThings = new List<Something>
    {
        new Something { SequenceNumber = 1 },
        new Something { SequenceNumber = 2 },
        new Something { SequenceNumber = 4 },
        new Something { SequenceNumber = 3 },
    }

    // Assert that a sequence of integer are incremental, that there are no repetitions or gaps.

    Assert....?
}

Row-wise pie plots

编辑:

要仅绘制特定行,请使用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> 选择要绘制的行(例如,第0行)。

.iloc

请参阅Indexing and Selecting Data上的文档