我试图将Pichart添加到Excel文件中

发布于 2025-01-21 12:55:24 字数 8 浏览 0 评论 0原文

continue

i'm trying to add the piechart to an excel sheet and i can't seem to understand what i've done wrong

  game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17',  'FIFA Online 3','FIFA 20' ,'Madden NFL 2017','NBA 2K18','FIFA Online 4']
    money=['9171818.72 ', '3242642.34 ', '2233697.69', '2233697.69', '1482612.63',' 1434341.90', '1144673.72 ', ' 1004000.00','1000000.00']

explode=(0.1,0,0,0,0,0,0,0,0)
plt.style.use('ggplot')
plt.title('les jeux ayant le plus de gain')
fig3= plt.pie(x=money, explode=explode, labels=game_name, shadow=True, startangle=90,autopct='%.2f%%')
plt.axis('equal')  
plt.legend(loc='upper left')
plt.show()
#plt.savefig('piechart.png')
sht.pictures.add(
    fig3,
    left=sht.range("A103").left,
    top= sht.range("A103").top,
    height=300,
    width=500,
)

i'm gettiing this error "don't know what to do with that image object"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-01-28 12:55:25

使用.insert_image()

import xlsxwriter
from matplotlib import pyplot as plt


game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17',  'FIFA Online 3','FIFA 20' ,'Madden NFL 2017','NBA 2K18','FIFA Online 4']
money=['9171818.72 ', '3242642.34 ', '2233697.69', '2233697.69', '1482612.63',' 1434341.90', '1144673.72 ', ' 1004000.00','1000000.00']

explode=(0.1,0,0,0,0,0,0,0,0)
plt.style.use('ggplot')
plt.title('les jeux ayant le plus de gain')
fig3= plt.pie(x=money, explode=explode, labels=game_name, shadow=True, startangle=90,autopct='%.2f%%')
plt.axis('equal')  
plt.legend(loc='upper left')
plt.savefig('piechart.png')

# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('images.xlsx')
sht = workbook.add_worksheet()

sht.insert_image('A103', 'piechart.png')
workbook.close()

use .insert_image()

import xlsxwriter
from matplotlib import pyplot as plt


game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17',  'FIFA Online 3','FIFA 20' ,'Madden NFL 2017','NBA 2K18','FIFA Online 4']
money=['9171818.72 ', '3242642.34 ', '2233697.69', '2233697.69', '1482612.63',' 1434341.90', '1144673.72 ', ' 1004000.00','1000000.00']

explode=(0.1,0,0,0,0,0,0,0,0)
plt.style.use('ggplot')
plt.title('les jeux ayant le plus de gain')
fig3= plt.pie(x=money, explode=explode, labels=game_name, shadow=True, startangle=90,autopct='%.2f%%')
plt.axis('equal')  
plt.legend(loc='upper left')
plt.savefig('piechart.png')

# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('images.xlsx')
sht = workbook.add_worksheet()

sht.insert_image('A103', 'piechart.png')
workbook.close()
花伊自在美 2025-01-28 12:55:25

如果要添加一个饼图以表现出色,则可以使用XLSXWriter直接进行操作,而不是添加图像。像这样的东西:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart_pie.xlsx')
worksheet = workbook.add_worksheet()

bold = workbook.add_format({'bold': 1})
money_format = workbook.add_format({'num_format': '$#,##0'})

# Add the worksheet data that the charts will refer to.
headings = ['Game', 'Earnings']

game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17', 'FIFA Online 3',
             'FIFA 20' ,'Madden NFL 2017', 'NBA 2K18', 'FIFA Online 4']
money=[9171818.72 , 3242642.34 , 2233697.69, 2233697.69, 1482612.63,
       1434341.90, 1144673.72, 1004000.00, 1000000.00]

worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', game_name)
worksheet.write_column('B2', money)

# Create a new chart object.
chart = workbook.add_chart({'type': 'pie'})

# Configure the series.
max_row = len(game_name)
chart.add_series({
    'categories': ['Sheet1', 1, 0, max_row, 0],
    'values':     ['Sheet1', 1, 1, max_row, 1],
})

# Add a title.
chart.set_title({'name': 'Les jeux ayant le plus de gain'})

# Set an Excel chart style. Colors with white outline and shadow.
chart.set_style(10)

# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)

# Set the first column width for clarity.
worksheet.set_column(0, 0, 15)

# Set the second column witch and format for currency.
worksheet.set_column(1, 1, 10, money_format)

workbook.close()

输出

”在此处输入图像描述”

If you want to add a pie chart to Excel you could do it directly using XlsxWriter, rather than adding an image. Something like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart_pie.xlsx')
worksheet = workbook.add_worksheet()

bold = workbook.add_format({'bold': 1})
money_format = workbook.add_format({'num_format': '$#,##0'})

# Add the worksheet data that the charts will refer to.
headings = ['Game', 'Earnings']

game_name = ['Rocket League','FIFA 19','FIFA 18', 'FIFA 17', 'FIFA Online 3',
             'FIFA 20' ,'Madden NFL 2017', 'NBA 2K18', 'FIFA Online 4']
money=[9171818.72 , 3242642.34 , 2233697.69, 2233697.69, 1482612.63,
       1434341.90, 1144673.72, 1004000.00, 1000000.00]

worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', game_name)
worksheet.write_column('B2', money)

# Create a new chart object.
chart = workbook.add_chart({'type': 'pie'})

# Configure the series.
max_row = len(game_name)
chart.add_series({
    'categories': ['Sheet1', 1, 0, max_row, 0],
    'values':     ['Sheet1', 1, 1, max_row, 1],
})

# Add a title.
chart.set_title({'name': 'Les jeux ayant le plus de gain'})

# Set an Excel chart style. Colors with white outline and shadow.
chart.set_style(10)

# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)

# Set the first column width for clarity.
worksheet.set_column(0, 0, 15)

# Set the second column witch and format for currency.
worksheet.set_column(1, 1, 10, money_format)

workbook.close()

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文