Created
February 10, 2021 14:51
-
-
Save t2y/bf0ffcf0ff1546f66e18caebcfe06cff to your computer and use it in GitHub Desktop.
pie chart with datalabellist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from openpyxl import Workbook | |
from openpyxl.chart import PieChart, Reference | |
from openpyxl.chart.label import DataLabelList | |
wb = Workbook() | |
ws = wb.active | |
df = pd.read_csv('population.csv') | |
ws.append(df.columns.tolist()) | |
for row in df.values: | |
ws.append(list(row)) | |
row_length = 1 + len(df.values) | |
data = Reference(ws, min_col=2, max_col=2, min_row=2, max_row=row_length) | |
categories = Reference(ws, min_col=1, max_col=1, min_row=2, max_row=row_length) | |
dLbls = DataLabelList() | |
dLbls.showPercent = True | |
dLbls.showVal = True | |
pie = PieChart(dLbls=dLbls) | |
pie.add_data(data, titles_from_data=True) | |
pie.set_categories(categories) | |
pie.title = '都道府県別の人口' | |
ws.add_chart(pie, 'A9') | |
wb.save('population_pie_percent.xlsx') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment