Learn how to Create Tornado Charts in Excel using Python.
Created
July 9, 2024 11:47
-
-
Save aspose-com-gists/a09b94108183edfebaaab914d09aaab1 to your computer and use it in GitHub Desktop.
Create Tornado Charts in Excel using Python
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 aspose.cells | |
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType, LegendPositionType, TickLabelPositionType, LabelPositionType | |
from aspose.pydrawing import Color | |
# Load an existing Excel file with data | |
wb = Workbook("sample.xlsx") | |
# Access the worksheet | |
sheet = wb.worksheets[0] | |
# Get charts | |
charts = sheet.charts | |
# Add bar chart | |
index = charts.add(ChartType.BAR_STACKED, 8, 1, 24, 8) | |
chart = charts[index] | |
# Set data for bar chart | |
chart.set_chart_data_range("A1:C7", True) | |
# Set properties for bar chart | |
chart.title.text = "Tornado chart" | |
chart.style = 2 | |
chart.plot_area.area.foreground_color = Color.white | |
chart.plot_area.border.color = Color.white | |
chart.legend.position = LegendPositionType.BOTTOM | |
chart.category_axis.tick_label_position = TickLabelPositionType.LOW | |
chart.category_axis.is_plot_order_reversed = True | |
chart.gap_width = 10 | |
value_axis = chart.value_axis | |
value_axis.tick_labels.number_format = "#,##0;#,##0" | |
# Save the file | |
wb.save("TornadoChart_out.xlsx") |
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 aspose.cells | |
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType, LegendPositionType, TickLabelPositionType, LabelPositionType | |
from aspose.pydrawing import Color | |
# Create a new Excel Workbook | |
wb = Workbook() | |
worksheet = wb.worksheets[0] | |
# Add sample values to cells | |
worksheet.cells.get("A1").put_value("Products") | |
worksheet.cells.get("A2").put_value("Product A") | |
worksheet.cells.get("A3").put_value("Product B") | |
worksheet.cells.get("A4").put_value("Product C") | |
worksheet.cells.get("A5").put_value("Product D") | |
worksheet.cells.get("A6").put_value("Product E") | |
worksheet.cells.get("B1").put_value("2021-2022") | |
worksheet.cells.get("B2").put_value(-100) | |
worksheet.cells.get("B3").put_value(-80) | |
worksheet.cells.get("B4").put_value(-75) | |
worksheet.cells.get("B5").put_value(-60) | |
worksheet.cells.get("B6").put_value(-48) | |
worksheet.cells.get("C1").put_value("2023-2024") | |
worksheet.cells.get("C2").put_value(95) | |
worksheet.cells.get("C3").put_value(80) | |
worksheet.cells.get("C4").put_value(72) | |
worksheet.cells.get("C5").put_value(65) | |
worksheet.cells.get("C6").put_value(45) | |
charts = worksheet.charts | |
# Add bar chart | |
index = charts.add(ChartType.BAR_STACKED, 8, 1, 24, 8) | |
chart = charts[index] | |
# Set data for bar chart | |
chart.set_chart_data_range("A1:C6", True) | |
# Set properties for bar chart | |
chart.title.text = "Tornado chart" | |
chart.style = 2 | |
chart.plot_area.area.foreground_color = Color.white | |
chart.plot_area.border.color = Color.white | |
chart.legend.position = LegendPositionType.BOTTOM | |
chart.category_axis.tick_label_position = TickLabelPositionType.LOW | |
chart.category_axis.is_plot_order_reversed = True | |
chart.gap_width = 50 | |
# # Show data labels | |
for series in chart.n_series: | |
series.border.color = Color.black | |
datalabels = series.data_labels | |
# Set the position of DataLabels | |
datalabels.position = LabelPositionType.CENTER | |
# Show the value in the DataLabels | |
datalabels.show_value = True | |
datalabels.font.color = Color.white | |
datalabels.number_format = "#,##0;#,##0" | |
value_axis = chart.value_axis | |
value_axis.tick_labels.number_format = "#,##0;#,##0" | |
# Save the file | |
wb.save("insertAndCreate_out.xlsx") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment