如何在Jupyter Python中消除饼图之前的文本

时间:2018-12-07 14:51:26

标签: python jupyter-notebook jupyter jupyter-lab

我正在尝试根据此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')])

This is the bar chart that is returned then

1 个答案:

答案 0 :(得分:0)

您可以在plt.pie(...);调用的末尾添加分号。