【问题标题】:how to display the plotting pie chart into excel file beside the create table using dataframe and python如何使用数据框和python将绘图饼图显示到创建表旁边的excel文件中
【发布时间】:2020-11-26 16:21:48
【问题描述】:

我有一个 Python 脚本,它通过向现有表中添加一个新字段来预测数据帧正面和负面情绪的情绪分析。我绘制了正面和负面情绪百分比的饼图。最后将结果保存到excel文件中。

我想在表格旁边添加这个绘制饼图。

这个任务怎么做?

代码:

import pandas as pd
import matplotlib.pyplot as plt

    test_twtr = pd.read_excel("F:/AIenv/sentiment_analysis/20200717_100219_Sentiment - Copy.xlsx",names=col_names)
    
    test_twtr['processed_TEXT'] = test_twtr['TRANSLATION'].apply(processTweet)
    test_twtr_preds = LR_Model.predict(test_twtr['processed_TEXT'])
    def_test_twtr_preds = test_twtr.copy()
    positives = def_test_twtr_preds["predictions"][def_test_twtr_preds.predictions ==1]
    negatives = def_test_twtr_preds["predictions"][def_test_twtr_preds.predictions ==0]
    
    slices_tweets = [format(100*len(positives)/len(def_test_twtr_preds["predictions"])), format(100*len(negatives)/len(def_test_twtr_preds["predictions"]))]
    analysis = ['pos', 'neg']
    colors = ['yellow', 'red']
    plt.pie(slices_tweets, labels=analysis, startangle=90, autopct='%.1f%%',colors = colors)
    plt.title("Percentage of sentiment analysis")
    plt.show()
    
    df_test_tweet_preds_to_csv = def_test_twtr_preds.to_csv(outputPath, index = False, header=True)
     

【问题讨论】:

    标签: python excel pandas matplotlib


    【解决方案1】:

    我认为您不能使用 Pandas 直接将 matplot 图像插入 Excel 文件。 您可以做一些解决方法:

    1. 保存图片
    2. 将数据导出到 excel
    3. 用 xlsxwriter 打开 excel 文件
    4. 并将图像加载到 Excel 文件中。并再次保存

    有关如何将图像插入 excel 的信息,请参阅: https://xlsxwriter.readthedocs.io/example_images.html

    【讨论】:

    猜你喜欢
    • 2017-12-30
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2023-03-08
    • 2018-08-05
    • 2019-06-20
    相关资源
    最近更新 更多