如何使饼图标签与数据框中的正确数值对齐?

huangapple go评论87阅读模式
英文:

How do i get pie chart labels to line up with the correct values from dataframe?

问题

我的饼图看起来还可以,各个部分的大小是正确的,但标签位置不正确。

# 声明爆炸式饼图
explode = [0, 0.1, 0, 0, 0, 0]
# 定义要使用的Seaborn颜色调色板
palette_color = sns.color_palette('pastel')

# 在图表上绘制数据
fig=plt.pie(combined_df.groupby(['Continent'])['total_consumption'].sum(), colors=palette_color, labels=combined_df['Continent'].unique(),
            explode=explode, autopct='%.0f%%', labeldistance=0.9)
plt.show

应该绘制的值:

  • 非洲 227.0
  • 亚洲 128.7
  • 欧洲 431.9
  • 北美洲 147.5
  • 大洋洲 42.5
  • 南美洲 83.2

如何使饼图标签与数据框中的正确数值对齐?

英文:

My pie chart is coming out okay, the segments are the correct sizes, but the labels aren't on the correct places.

# declaring exploding pie
explode = [0, 0.1, 0, 0, 0,0]
# define Seaborn color palette to use
palette_color = sns.color_palette('pastel')
  
# plotting data on chart
fig=plt.pie(combined_df.groupby(['Continent'])['total_consumption'].sum(), colors=palette_color,labels=combined_df['Continent'].unique(),
        explode=explode, autopct='%.0f%%', labeldistance=0.9,)
plt.show

如何使饼图标签与数据框中的正确数值对齐?

the values that should be plotted

Continent
Africa           227.0
Asia             128.7
Europe           431.9
North America    147.5
Oceania           42.5
South America     83.2

答案1

得分: 3

我怀疑unique()value_counts()返回结果的顺序不一致。

value_counts()返回结果按计数排序,而unique()按在数组中的出现顺序排序。

你可以尝试这样做:

value_cnts = combined_df['Continent'].value_counts()
fig = plt.pie(value_cnts, colors=palette_color, labels=value_cnts.index, 
    explode=explode, autopct='%.0f%%', labeldistance=0.9)
英文:

My suspicion is that the order of the returned results of unique() and value_counts() is not consistent.

value_counts() returned is order by count, and unique() is order by appearance in the array.

You can try this:

value_cnts = combined_df['Continent'].value_counts()
fig = plt.pie(value_cnts, colors=palette_color, labels=value_cnts.index, 
    explode=explode, autopct='%.0f%%', labeldistance=0.9)

答案2

得分: 1

尝试不对 value_counts 的值进行排序:

fig = plt.pie(combined_df['Continent'].value_counts(sort=False),  # <- 不排序
            colors=palette_color,
            labels=combined_df['Continent'].unique(),  # 因为 unique 不排序
            explode=explode, autopct='%.0f%%', labeldistance=0.9,)

演示:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

palette_color = sns.color_palette('pastel')
explode = [0, 0.1, 0, 0, 0,0]

np.random.seed(2023)
data = np.random.choice(
    ['Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South America'],
    p=(0.213989, 0.121324, 0.407146, 0.139046, 0.040064, 0.078431),
    size=1000
)
combined_df = pd.DataFrame({'Continent': data})

fig = plt.pie(combined_df['Continent'].value_counts(sort=False), 
              autopct='%.0f%%', labeldistance=0.9, 
              labels=combined_df['Continent'].unique(),
              colors=palette_color, explode=explode)
plt.show()

如何使饼图标签与数据框中的正确数值对齐?

英文:

Try to not sort values on value_counts:

fig=plt.pie(combined_df['Continent'].value_counts(sort=False),  # <- do not sort
            colors=palette_color,
            labels=combined_df['Continent'].unique(),  # because unique is not sorted
            explode=explode, autopct='%.0f%%', labeldistance=0.9,)

Demo:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

palette_color = sns.color_palette('pastel')
explode = [0, 0.1, 0, 0, 0,0]

np.random.seed(2023)
data = data = np.random.choice(
    ['Africa', 'Asia', 'Europe', 'North America', 'Oceania', 'South America'],
    p=(0.213989, 0.121324, 0.407146, 0.139046, 0.040064, 0.078431),
    size=1000
)
combined_df = pd.DataFrame({'Continent': data})

fig = plt.pie(combined_df['Continent'].value_counts(sort=False), 
              autopct='%.0f%%', labeldistance=0.9, 
              labels=combined_df['Continent'].unique(),
              colors=palette_color, explode=explode)
plt.show()

如何使饼图标签与数据框中的正确数值对齐?

huangapple
  • 本文由 发表于 2023年1月9日 15:28:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定