首页 > 解决方案 > 如何在 Jupyter Python 中去除饼图之前的文本

问题描述

我正在尝试使用此 CSV 文件中的数据制作饼图。问题是它可以工作,但它在饼图之前显示文本,我想摆脱它。

另外,如何缩放条形图使其完全适合工作簿?

#imports for pandas and numpy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import*
from collections import OrderedDict
from collections import Counter

#Importing the file where I get the data from
df = pd.read_csv('file.csv',delimiter=',', skipinitialspace = True)

df['artist genre'].value_counts()

genreArray = np.array(df['artist genre'])


keys = Counter(genreArray).keys()
values = Counter(genreArray).values()
#counts =Counter(genreArray)

plt.pie([float(v) for v in values], labels=[k for k in keys],
          autopct=None,frame=True)

结果:

([<matplotlib.patches.Wedge at 0x28d57bec160>,
  <matplotlib.patches.Wedge at 0x28d57bec6a0>,
  <matplotlib.patches.Wedge at 0x28d57becbe0>,
  <matplotlib.patches.Wedge at 0x28d57bf5160>,
  <matplotlib.patches.Wedge at 0x28d57bf56a0>,
  <matplotlib.patches.Wedge at 0x28d57bf5be0>,
  <matplotlib.patches.Wedge at 0x28d57bfc160>,
  <matplotlib.patches.Wedge at 0x28d57bfc6a0>,
  <matplotlib.patches.Wedge at 0x28d57bfcc88>,
  <matplotlib.patches.Wedge at 0x28d57c09208>,
  <matplotlib.patches.Wedge at 0x28d5796acc0>,
  <matplotlib.patches.Wedge at 0x28d57c09c50>,
  <matplotlib.patches.Wedge at 0x28d57c101d0>,
  <matplotlib.patches.Wedge at 0x28d57c10710>],
 [Text(0.801866,0.753002,'Pop'),
  Text(-0.646564,0.889919,'Hip Hop'),
  Text(-1.08052,-0.206119,'Electronic Dance Music'),
  Text(-0.753002,-0.801865,'Latin Pop'),
  Text(-0.273559,-1.06544,'R&B'),
  Text(0.172078,-1.08646,'Pop Rock'),
  Text(0.339919,-1.04616,'Electronic Music'),
  Text(0.468357,-0.99531,'Deep House'),
  Text(0.701166,-0.847565,'Reggaeton'),
  Text(0.946816,-0.559946,'Progressive House'),
  Text(1.04616,-0.339919,'Dance Pop'),
  Text(1.08052,-0.20612,'Electropop'),
  Text(1.09512,-0.103519,'Country'),
  Text(1.09946,-0.0345519,'Indie Pop')])

这是然后返回的条形图

标签: pythonjupyter-notebookjupyterjupyter-lab

解决方案


plt.pie(...);您可以在通话结束时添加分号。


推荐阅读