{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "2KNR_uhp2Iz2" }, "source": [ "# matplotlib\n", "\n", "グラフなどの可視化に用いる最も一般的なPythonのライブラリです。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "executionInfo": { "elapsed": 18, "status": "aborted", "timestamp": 1656663646874, "user": { "displayName": "Yuya Shibuya", "userId": "13278923316285788453" }, "user_tz": -540 }, "id": "C4bVxO602Iz2" }, "outputs": [], "source": [ "import pandas as pd\n", "import random\n", "from matplotlib import pyplot as plt\n", "import numpy as np\n", "%matplotlib inline\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 基本的な図の作り方\n", "\n", "線グラフを描いてみます。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]\n", "yen = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]\n", "\n", "plt.plot(years, yen, color = 'green', marker = 'o', linestyle = 'solid')\n", "plt.title('Random price plot')\n", "plt.xlabel('year')\n", "plt.ylabel('yen')\n", "plt.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "`pandas`のデータフレームを用いて棒グラフをつくります。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fruit = pd.DataFrame({'category': ['banana','orange','apple','apple','orange','orange','apple','apple','apple','banana'],\n", " \"num_sell\" : [100, 40, 30, 30, 40, 100, np.nan, 40, 1, 6],\n", " \"price\" : [100, 130, 80, 150, 70, 75, np.nan, 90, 190, 110],})\n", "fruit" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "executionInfo": { "elapsed": 18, "status": "aborted", "timestamp": 1656663646874, "user": { "displayName": "Yuya Shibuya", "userId": "13278923316285788453" }, "user_tz": -540 }, "id": "UGxyShDx2Iz2" }, "outputs": [], "source": [ "fruit_size = fruit.groupby(['category']).sum()\n", "fruit_size" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "executionInfo": { "elapsed": 2694, "status": "aborted", "timestamp": 1656663646875, "user": { "displayName": "Yuya Shibuya", "userId": "13278923316285788453" }, "user_tz": -540 }, "id": "H6b1L_682Iz2" }, "outputs": [], "source": [ "plt.bar(fruit_size.index, fruit_size.num_sell, color = ['yellow','red','orange'], alpha =0.7)\n", "plt.ylabel('# of sold food')\n", "plt.title('Fruit')\n", "# plt.savefig('fig_soldfruit.jpg') # この1行を実行するとカレントディレクトリにプロットした画像を保存することができます。\n", "plt.show()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 文字をプロットする" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | text | \n", "freq | \n", "importance | \n", "
---|---|---|---|
0 | \n", "data | \n", "100 | \n", "200 | \n", "
1 | \n", "python | \n", "40 | \n", "180 | \n", "
2 | \n", "analysis | \n", "80 | \n", "190 | \n", "
3 | \n", "science | \n", "21 | \n", "50 | \n", "
4 | \n", "statistics | \n", "10 | \n", "150 | \n", "
5 | \n", "dynamic | \n", "3 | \n", "30 | \n", "
6 | \n", "r | \n", "45 | \n", "20 | \n", "
7 | \n", "machine learning | \n", "79 | \n", "40 | \n", "
8 | \n", "deep learning | \n", "81 | \n", "20 | \n", "
9 | \n", "insights | \n", "3 | \n", "90 | \n", "