MatplotlibとSeabornによる高度なプロット
メニューを表示するにはスワイプしてください
空間データを扱う際、効果的な可視化はパターンの発見やインサイトの伝達に不可欠。Python の matplotlib および seaborn ライブラリは、空間プロットを強化するための強力なツールを提供し、色のカスタマイズ、情報量の多い凡例の追加、説明的なタイトルの設定、複数データレイヤーの重ね合わせが可能。これらの手法を習得することで、重要な空間的関係を強調する明確で説得力のある地図や可視化を作成可能。
空間プロットのカスタマイズは、データの外観制御から始まることが多い。異なるカテゴリや値を表す色の指定、重なり合う特徴を明らかにする透明度の調整、視覚的エンコーディングを説明する凡例の利用が可能。タイトルや軸ラベルの追加は、プロットの迅速な解釈を助ける。複数レイヤー(例:行政境界上に道路を重ねる)の重ね合わせにより、空間分析にさらなる文脈を付与。
12345678910111213141516171819202122232425262728293031323334import geopandas as gpd import matplotlib.pyplot as plt url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" world = gpd.read_file(url) # Create a plot with customizations fig, ax = plt.subplots(figsize=(12, 8)) # Plot countries, coloring by continent world.plot(column='CONTINENT', cmap='Set2', edgecolor='black', linewidth=0.5, legend=True, ax=ax, alpha=0.8) # Overlay country boundaries with a thicker line for emphasis world.boundary.plot(ax=ax, color='gray', linewidth=1.5) # Add a title and axis labels ax.set_title('World Countries by Continent', fontsize=18) ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') # Customize legend position so it doesn't overlap the map leg = ax.get_legend() if leg: leg.set_bbox_to_anchor((1.15, 0.5)) plt.tight_layout() plt.show()
注意
公式データセットのカラム名は CONTINENT
地図ベースの可視化に加えて、面積、人口、密度などの空間属性の分布を統計プロットで分析することも有効です。Seabornはこの目的に特に適しており、ヒストグラムや散布図などを、魅力的なデフォルトスタイルと簡単なカスタマイズで作成できます。seabornと空間データを組み合わせることで、データセット内の傾向や外れ値を迅速に特定でき、より深い空間分析を支援します。
1234567891011121314151617181920212223242526272829303132import geopandas as gpd import seaborn as sns import matplotlib.pyplot as plt # Load sample data directly from the official URL url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" world = gpd.read_file(url) # The official dataset doesn't have a pre-made 'area' column, so we calculate it. # Note: This calculates area in decimal degrees based on the WGS84 projection. world['area'] = world.geometry.area # Plot a histogram of country areas plt.figure(figsize=(10, 6)) # 'area' is our new custom column, kde=True adds the density curve sns.histplot(world['area'], bins=30, color='skyblue', kde=True) plt.title('Distribution of Country Areas') plt.xlabel('Area (sq. degrees)') plt.ylabel('Number of Countries') plt.tight_layout() plt.show() # Plot a scatter plot of population vs. area plt.figure(figsize=(10, 6)) # Adjusted column names to match the official dataset: 'POP_EST' and 'CONTINENT' sns.scatterplot(x='area', y='POP_EST', data=world, hue='CONTINENT', palette='Set2') plt.title('Country Population vs. Area by Continent') plt.xlabel('Area (sq. degrees)') plt.ylabel('Estimated Population') plt.legend(title='Continent', bbox_to_anchor=(1.05, 1), loc='upper left') plt.tight_layout() plt.show()
すべて明確でしたか?
フィードバックありがとうございます!
セクション 2. 章 4
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 2. 章 4