Anaconda:ImportError:无法从“ PIL”导入名称“ _imaging”matplotlib.pyplot

问题描述

我正在尝试使用matplotlib.pyplot库绘制直方图。

from matplotlib import pyplot as plt
plt.hist(df["xxx"])

当我尝试导入该库时,出现错误

ImportError:无法从“ PIL”(C:\ Users \ Taras \ AppData \ Roaming \ Python \ python38 \ site-packages \ PIL_ init )导入名称成像” > .py)

我正在使用Anaconda和Jupyter Notebook。

解决方法

尝试一下此代码,因为它变得非常复杂

import matplotlib.pyplot as plt
%matplotlib inline
plt.hist(df["xxx"])
,

您需要将代码的第一行更改为以下内容:

import matplotlib.pyplot as plt

第二行代码还可以,但是我不明白第三行代码在做什么。您要打印什么数据框?我从网上复制并粘贴了一个数据框,然后运行您的代码来更改代码的第一行和第三行。

在使用Python内核的Jupyter Lab中一切运行良好。

import matplotlib.pyplot as plt
from matplotlib import pyplot    

df = pd.DataFrame({
    'name':['john','mary','peter','jeff','bill','lisa','jose'],'age':[23,78,22,19,45,33,20],'gender':['M','F','M','M'],'state':['california','dc','california','texas','texas'],'num_children':[2,3,2,1,4],'num_pets':[5,5,3]
})

df.plot(kind='hist',x='num_children',y='num_pets',color='red')
plt.show()

可以在下面的链接中查看图。

link

,

你可以通过手动导入Image lib来解决这个问题

import PIL
from PIL import Image
import matplotlib.pyplot as plt