I can create a piechart where each wedges is having its size printed like this:
df = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],
'radius': [2439.7, 6051.8, 6378.1]},
index=['Mercury', 'Venus', 'Earth'])
plot = df.plot.pie(y='mass', figsize=(5, 5), autopct= '%.2f')
How can I make it print the values only for a subset (say don't print Mercury)?
CodePudding user response:
- Use a
lambda
function to conditionally place values withautopct
. As perCodePudding user response:
One option would be to remove the text (or set it to not visible) after creating the plot. The main issue now is finding which is the correct text to modify. For example:
import pandas as pd df = pd.DataFrame({'mass': [0.330, 4.87 , 5.97], 'radius': [2439.7, 6051.8, 6378.1]}, index=['Mercury', 'Venus', 'Earth']) plot = df.plot.pie(y='mass', figsize=(5, 5), autopct= '%.2f') print(plot.texts)
would yield
[Text(1.09526552098778, 0.10194821496900702, 'Mercury'), Text(0.5974175569024254, 0.05560811725582201, '2.95'), Text(0.01701519685165565, 1.0998683935253797, 'Venus'), Text(0.009281016464539445, 0.5999282146502071, '43.60'), Text(-0.11887821664562105, -1.0935574834489301, 'Earth'), Text(-0.0648426636248842, -0.5964859000630527, '53.45')]
So you can see, it is the second
Text
item we want to turn off. A simple way to do this is:plot.texts[1].set_visible(False)
or alternatively
plot.texts[1].remove()
Obviously, then the issue in generalising this is to know which texts to remove in advance. As you cans see above the order in which texts are added to the axes are the index name (planet name), then the autopct label for that planet, then repeat for the next planet. So if you know you want to remove the autopct label for planet
i
, you could use something likeplot.texts[2 * i 1].remove()
.A helper function to do this for a given planet label would look something like this:
def remove_text(label): ind = df.index.get_loc(label) * 2 1 plot.texts[ind].remove() remove_text('Mercury')