Python 3.10生成词云遇Pillow textsize弃用警告,求解决方法
问题
在Python 3.10环境中生成词云时,笔记本反复弹出大量如下弃用警告:
DeprecationWarning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.
相同代码在Python 3.7环境运行时无此警告,所用代码如下:
def wordcloud(self) -> None: """Function to generate wordcloud""" long_string = ','.join(self.corpus) # Create a WordCloud object wordcloud = WordCloud(background_color="white", max_words=5000, contour_width=3, contour_color='steelblue') # Generate a word cloud wordcloud.generate(long_string) # Visualize the word cloud plt.figure(figsize=(10, 5)) plt.imshow(wordcloud, interpolation="bilinear") plt.axis('off') plt.show()
解决方法
升级wordcloud库:警告根源是旧版wordcloud调用了Pillow高版本(≥9.1.0)中已弃用的
textsize方法,升级到最新版wordcloud即可解决兼容问题,执行命令:pip install --upgrade wordcloud临时屏蔽警告(仅应急使用):如果暂时无法升级库,可在代码开头添加以下代码屏蔽该特定警告:
import warnings warnings.filterwarnings("ignore", category=DeprecationWarning, message="textsize is deprecated and will be removed in Pillow 10")注意:此方法仅隐藏警告,未解决根本问题,当Pillow升级到10.x版本后,旧版wordcloud可能直接报错,因此优先推荐升级wordcloud。
内容的提问来源于stack exchange,提问作者Shiva
相关产品推荐
相关产品推荐

