diff --git a/Matplotlib/Matplotlib_Create_Piechart.ipynb b/Matplotlib/Matplotlib_Create_Piechart.ipynb new file mode 100644 index 0000000000..530856853e --- /dev/null +++ b/Matplotlib/Matplotlib_Create_Piechart.ipynb @@ -0,0 +1,293 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "8c9ef857-6c86-47f6-8f65-57721efda695", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "cf7eb0f0-1f89-497f-bc54-a5fd83fc5a02", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Matplotlib - Create Piechart" + ] + }, + { + "cell_type": "markdown", + "id": "fa0d95b2-8af8-4f15-9f82-d2dbc77d2ada", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #matplotlib #piechart #datavisualization #plotting #python #visualization" + ] + }, + { + "cell_type": "markdown", + "id": "fb885501-a3c0-4b63-92df-c01fd3dae1c8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [SaiKiran M](www.linkedin.com/in/msaikiran9)" + ] + }, + { + "cell_type": "markdown", + "id": "36705a1a-241c-483a-8735-a32b35a1d18f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-10-05 (Created: 2023-10-05)" + ] + }, + { + "cell_type": "markdown", + "id": "427a04f7-9e13-4c0c-bb0d-94a8d6f81bdb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook will show how to create a piechart with Matplotlib. Pie charts are useful to represent proportions of a whole." + ] + }, + { + "cell_type": "markdown", + "id": "45c4bd95-2f4d-4574-8ded-cd8b6f04348a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n", + "- [Matplotlib Pie Chart](https://matplotlib.org/stable/plot_types/stats/pie.html#sphx-glr-plot-types-stats-pie-py)" + ] + }, + { + "cell_type": "markdown", + "id": "042761b8-c31a-4bb6-8f57-eaca1a012cf0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "9b10f380-6ddc-48f5-90b4-9454ab324326", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "435a47b7-7012-4172-8565-a8b30ded1700", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import naas" + ] + }, + { + "cell_type": "markdown", + "id": "a8fa608d-e932-4819-9a1b-50e0977daa65", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `labels`: list of labels for each slice of the pie chart\n", + "- `sizes`: list of sizes for each slice of the pie chart\n", + "- `fig_path`: The file path or name for the output image file of the graph, saved as \"fig.png\"." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c87fee1-24bb-469a-9ccc-313ae6452c9f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "# Inputs\n", + "labels = [\"hotdogs\", \"salads\", \"sandwich\", \"burger\"] #visualize shares of food of a hotel by giving labels.\n", + "sizes = [14.5, 30.5, 45, 10] #here hotdogs shares corresponds to 14.5 and so on.\n", + "\n", + "# Outputs\n", + "fig_path = \"pie.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "aff0bef4-284d-4fe2-9a0d-67ce8505bf9d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "161eebd5-22b5-4d16-9791-46e6c457c163", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Create Pie Chart" + ] + }, + { + "cell_type": "markdown", + "id": "4b073715-6053-407e-888b-9a22a4f98b06", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "Create a pie chart with the given labels and sizes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "448ee5c8-7b82-474c-9608-3a6cb5745660", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "plt.pie(sizes, labels=labels, autopct='%1.1f%%')\n", + "plt.axis(\"equal\")\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "id": "2a8540d3-b242-40a4-ae03-f814472275a3", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "f7413927-a8ba-4f8b-9939-15d4e1b095e9", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Save figure" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "498629c1-6442-4694-bcdd-35ffd85a94a8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "plt.savefig(fig_path)" + ] + }, + { + "cell_type": "markdown", + "id": "0341a2a0-4905-45dd-a8c8-f1827cf7d212", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Share asset with naas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "063f9203-12cd-4705-b0cd-93cc9113acb4", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "naas.asset.add(fig_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "672cd7bd-98e3-451f-b99f-4d5e9a3e9750", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}