How can I add a constant percentage to each wedge of a pie chart using matplotlib
<p>Code snippet looks like this</p>

<pre><code>#df is dataframe with 6 rows, with each row index being the label of the sector
plt.pie(df.iloc[:,0], labels= df.index) #plot the first column of dataframe as a pie chart 
</code></pre>

<p>It generates a pie chart like this:</p>

<p><a href="http://i.stack.imgur.com/z9PXx.png" rel="nofollow">Pie Chart with the 6 sectors.</a></p>

<p>As you can see, the sectors kitchen &amp; entertainment are very small. I want to make each sector have a minimum value of 10 degree (1/36 %).</p>

<p>This would effectively mean fitting the data over 300 degrees (10 degree to each sector and we have 6 - Lighting, Entertainment, Kitchen, Cooling, Fridge, Others)</p>

<blockquote>
  <p>I want to make each sector have a minimum value of 10 degree</p>
</blockquote>

<p>Solving this for the general case is difficult and requires further definition. This introduces a skew into the results, and to decide between different algorithms, you'd need to specify a metric defining how close is some skewed result to the original. </p>

<p>However, there is a very simple thing you can do which will guarantee your requirement. Say the length of your DataFrame is <em>n</em>, the sum of its elements is <em>s</em>, and you want the result to have no less than <em>&alpha;</em> (in your case, <em>&alpha; = 1./36</em>). If you would simply add some <em>x</em> to all elements, then the new sum would be </p>

<p><em>s + nx</em></p>

<p>Assuming that no element is negative, the smallest element would have weight at least <em>x / (s + nx)</em>. </p>

<p>Solving for </p>

<p><em>x / (s + nx) = &alpha;</em></p>

<p>Gives</p>

<p><em>x = s &alpha; / (1 - &alpha;n)</em>.</p>

<p>So, if the Series you wish to pie-chart is <code>df.a</code>, you could do</p>

<pre><code>x = max(df.a.sum() * alpha / (1 - alpha * len(df)), 0)
</code></pre>

<p>and then plot instead <code>df.a + x</code>.</p>

<p><strong>Example</strong></p>

<pre><code>df = pd.DataFrame({'a': [1, 2, 3, 40, 40, 50, 50, 500]})

x = max(df.a.sum() * alpha / (1 - alpha * len(df)), 0)
</code></pre>

<p>You can check the smallest relative weight of <code>df.a + x</code></p>

<pre><code>(df.a + x).min() / (df.a + x).sum()
</code></pre>

<p>is indeed greater than <em>&alpha;</em>.</p>

python pandas matplotlib