{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 演習問題\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/kentakom1213/practice-datavisualization/blob/main/matplotlib_practice.ipynb)\n", "\n", "演習問題です。\n" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "# 日本語対応\n", "from matplotlib import rcParams\n", "rcParams['font.family'] = 'sans-serif'\n", "rcParams['font.sans-serif'] = ['Hiragino Maru Gothic Pro', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " longitude latitude housing_median_age total_rooms total_bedrooms \\\n", "0 -114.31 34.19 15.0 5612.0 1283.0 \n", "1 -114.47 34.40 19.0 7650.0 1901.0 \n", "2 -114.56 33.69 17.0 720.0 174.0 \n", "3 -114.57 33.64 14.0 1501.0 337.0 \n", "4 -114.57 33.57 20.0 1454.0 326.0 \n", "\n", " population households median_income median_house_value \n", "0 1015.0 472.0 1.4936 66900.0 \n", "1 1129.0 463.0 1.8200 80100.0 \n", "2 333.0 117.0 1.6509 85700.0 \n", "3 515.0 226.0 3.1917 73400.0 \n", "4 624.0 262.0 1.9250 65500.0 \n" ] } ], "source": [ "df = pd.read_csv('sample_data/california_housing_train.csv')\n", "\n", "print(df.head(5))" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
monthaverage_temperatureprecipitation_amount
0110.798.8
1211.8100.2
2313.069.4
3413.935.2
4515.413.3
5616.93.8
6717.70.0
7818.21.0
8918.21.9
91016.920.0
101113.450.3
111210.7105.9
\n", "
" ], "text/plain": [ " month average_temperature precipitation_amount\n", "0 1 10.7 98.8\n", "1 2 11.8 100.2\n", "2 3 13.0 69.4\n", "3 4 13.9 35.2\n", "4 5 15.4 13.3\n", "5 6 16.9 3.8\n", "6 7 17.7 0.0\n", "7 8 18.2 1.0\n", "8 9 18.2 1.9\n", "9 10 16.9 20.0\n", "10 11 13.4 50.3\n", "11 12 10.7 105.9" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# カリフォルニアの気象データ\n", "\n", "california_temp = pd.DataFrame(\n", " {\n", " \"month\": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " \"average_temperature\": [10.7, 11.8, 13.0, 13.9, 15.4, 16.9, 17.7, 18.2, 18.2, 16.9, 13.4, 10.7],\n", " \"precipitation_amount\": [98.8, 100.2, 69.4, 35.2, 13.3, 3.8, 0.0, 1.0, 1.9, 20.0, 50.3, 105.9],\n", " }\n", ")\n", "\n", "california_temp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 課題\n", "\n", "穴埋めで、下の図を完成させよう。\n", "\n", "1. 左上に`df['total_rooms']`と`df['total_bedrooms']`の箱ひげ図をプロット\n", "2. 右上に`df['total_rooms']`と`df['total_bedrooms']`のヒストグラムを重ねてプロット \n", " `bins=100`を指定すること\n", "3. 左下にカリフォルニアの雨温図をプロット \n", " (横軸:月, 縦軸:気温、降水量の折れ線グラフ)\n", "4. 右下にカリフォルニアのハイサーグラフをプロット \n", " ((x, y) = (気温, 降水量)のグラフ)\n", "\n", "↓こんな感じ\n", "\n", "```{figure} images/california_stats.png\n", "```" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (3114002290.py, line 12)", "output_type": "error", "traceback": [ "\u001b[0;36m Input \u001b[0;32mIn [67]\u001b[0;36m\u001b[0m\n\u001b[0;31m ax1. ...\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "### 問題 ###\n", "# ... に当てはまるコードを入れてね\n", "\n", "fig = plt.figure(figsize=(12, 10), dpi=100)\n", "\n", "ax1 = ...\n", "ax2 = ...\n", "ax3 = ...\n", "ax4 = ...\n", "\n", "# ax1 (左上)\n", "ax1. ...\n", "\n", "# ax2 (右上)\n", "ax2. ... # label='total_rooms'\n", "ax2. ... # label='total_bedrooms'\n", "ax2.legend(loc='upper right')\n", "\n", "# ax3 (左下)\n", "ax3. ... # label='temperature'\n", "ax3. ... # label='precipitation'\n", "ax3.legend(loc='upper right')\n", "\n", "# ax4 (右下)\n", "ax4. ...\n", "\n", "#### タイトル\n", "fig.suptitle('カリフォルニアの各種統計', fontsize=20)\n", "\n", "ax1.set_title('total_rooms and total_bedrooms')\n", "ax2.set_title('total_rooms and total_bedrooms')\n", "ax3.set_title('temperature and precippitation')\n", "ax4.set_title('hythergraph of california')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 解答\n", "\n", "
解答\n", "
\n", "\n", "```python\n", "fig = plt.figure(figsize=(12, 10), dpi=100)\n", "\n", "ax1 = fig.add_subplot(2, 2, 1)\n", "ax2 = fig.add_subplot(2, 2, 2)\n", "ax3 = fig.add_subplot(2, 2, 3)\n", "ax4 = fig.add_subplot(2, 2, 4)\n", "\n", "# ax1 (左上)\n", "ax1.boxplot((df['total_rooms'], df['total_bedrooms']), labels=('total_rooms', 'total_bedrooms'))\n", "\n", "# ax2 (右上)\n", "ax2.hist(df['total_rooms'], bins=100, label='total_rooms')\n", "ax2.hist(df['total_bedrooms'], bins=100, label='total_bedrooms')\n", "ax2.legend(loc='upper right')\n", "\n", "# ax3 (左下)\n", "ax3.plot(california_temp['month'], california_temp['average_temperature'], label=\"temperature\")\n", "ax3.plot(california_temp['month'], california_temp['precipitation_amount'], label=\"precipitation\")\n", "ax3.legend(loc='upper right')\n", "\n", "# ax4 (右下)\n", "ax4.plot(california_temp['average_temperature'], california_temp['precipitation_amount'])\n", "\n", "#### タイトル\n", "fig.suptitle('カリフォルニアの各種統計', fontsize=20)\n", "\n", "ax1.set_title('total_rooms and total_bedrooms')\n", "ax2.set_title('total_rooms and total_bedrooms')\n", "ax3.set_title('temperature and precippitation')\n", "ax4.set_title('hythergraph of california')\n", "\n", "plt.show()\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 参考\n", "\n", "[カリフォルニアの気象データ - 気象庁](https://www.data.jma.go.jp/cpd/monitor/climatview/graph_mkhtml_nrm.php?n=72494&m=1)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.2 ('.practice-datavisualization': venv)", "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.10.2" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "78d97a615b7eb856cc59c7df976d665ea878e9e1475b11b30e580e0add64b80f" } } }, "nbformat": 4, "nbformat_minor": 2 }