Chosen1& 2023-10-18 22:38 采纳率: 0%
浏览 325

ValueError: anchor not supported for multiline text请问这个错误怎么解决


import string
import re
import jieba
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
file = open(r"L:\Python-Code\smoker.txt", mode="r", encoding="utf-8")
txt1 = file.read()
txt2 = re.sub(r"【^\u4e00-\u9fa5】", "", txt1)
# txt2 是已经去除中文标点符号的政府工作报告文本
print(txt2)
txt3 = jieba.cut(txt2)
# 返回一个可迭代对象
txt4 = {}
for i in txt3:
    if i not in txt4:
        txt4[i] = 1
    else:
        txt4[i] += 1
print(txt4)
# 创建词语集合
txt5 = sorted(txt4.items(), key=lambda x: x[1], reverse=True)
print(txt5)
# 词语集合排序,得到一个列表
txt6 = {}
for word, count in txt5:
    txt6[word] = count
print(txt6)
print(type(txt6))
wordcloud = WordCloud(
    background_color="white",  # 背景颜色
    font_path="simkai.ttf",  # 字体路径
    max_words=20,  # 最多显示的单词数
    max_font_size=100,  # 单词最大字号
    width=500,  # 宽度
    height=500,  # 高度
)
wordcloud.generate_from_frequencies(txt6)
plt.figure(figsize=(8, 8))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off') # 关闭坐标轴
plt.show()

img


ValueError: anchor not supported for multiline text
请问这个错误怎么解决

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-19 13:17
    关注

    【以下回答由 GPT 生成】

    这个错误是因为在使用WordCloud生成词云图时,使用了一个不支持多行文本的参数。解决这个问题的方法是将生成词云图的代码放在一个try-except块中,并捕获ValueError异常。然后,在捕获异常的情况下,根据报错信息尝试使用其他参数替代。

    下面是修改后的代码:

    import string
    import re
    import jieba
    import numpy as np
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud
    
    file = open(r"L:\Python-Code\smoker.txt", mode="r", encoding="utf-8")
    txt1 = file.read()
    txt2 = re.sub(r"【^\u4e00-\u9fa5】", "", txt1)   # txt2 是已经去除中文标点符号的政府工作报告文本
    print(txt2)
    txt3 = jieba.cut(txt2)   # 返回一个可迭代对象
    txt4 = {}
    for i in txt3:
        if i not in txt4:
            txt4[i] = 1
        else:
            txt4[i] += 1
    print(txt4)
    txt5 = sorted(txt4.items(), key=lambda x: x[1], reverse=True)   # 创建词语集合并排序,得到一个列表
    print(txt5)
    txt6 = {}
    for word, count in txt5:
        txt6[word] = count
    print(txt6)
    print(type(txt6))
    
    try:
        wordcloud = WordCloud(
            background_color="white",  # 背景颜色
            font_path="simkai.ttf",  # 字体路径
            max_words=20,  # 最多显示的单词数
            max_font_size=100,  # 单词最大字号
            width=500,  # 宽度
            height=500,  # 高度
        )
        wordcloud.generate_from_frequencies(txt6)
        plt.figure(figsize=(8, 8))
        plt.imshow(wordcloud, interpolation='bilinear')
        plt.axis('off')  # 关闭坐标轴
        plt.show()
    except ValueError:
        # 根据报错信息尝试使用其他参数
        wordcloud = WordCloud(
            # 使用其他参数替代
        )
        wordcloud.generate_from_frequencies(txt6)
        plt.figure(figsize=(8, 8))
        plt.imshow(wordcloud, interpolation='bilinear')
        plt.axis('off')  # 关闭坐标轴
        plt.show()
    

    请根据报错信息修改WordCloud的参数,并按照上述代码示例中的try-except块的结构添加相应的异常处理代码。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 10月18日

悬赏问题

  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能