データ可視化の実践【matplotlibとplotlyで業務レポートを作る方法】

データ可視化の実践【matplotlibとplotlyで業務レポートを作る方法】 AIツール・レビュー

はじめに

データ分析の結果は「可視化」することで初めて意味を持ちます。「グラフは作れるが、見栄えが悪くて現場に伝わらない」という課題を抱える方は多いです。実務で使えるデータ可視化の実践テクニックをまとめます。

matplotlibの実務的な使い方

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import japanize_matplotlib
import pandas as pd
import numpy as np

# ビジネスレポート向けのスタイル設定
plt.rcParams.update({
    'figure.facecolor': 'white',
    'axes.spines.top': False,
    'axes.spines.right': False,
    'axes.grid': True,
    'grid.alpha': 0.3,
    'font.size': 12
})

# 月次売上のトレンドグラフ
months = pd.date_range('2025-01', periods=12, freq='MS')
revenue = np.random.randint(800, 1200, 12)
target = np.full(12, 1000)

fig, ax = plt.subplots(figsize=(12, 5))
ax.bar(months, revenue, width=25, alpha=0.8, label='実績', color='steelblue')
ax.plot(months, target, '--', color='red', linewidth=2, label='目標')
ax.set_title('月次売上推移(2025年)', fontsize=16, fontweight='bold')
ax.set_ylabel('売上(万円)')
ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f'{x:,.0f}'))
ax.legend()
plt.tight_layout()
plt.savefig('monthly_revenue.png', dpi=150, bbox_inches='tight')
plt.show()

plotlyでインタラクティブなグラフを作る

pip install plotly
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# データ準備
df = pd.DataFrame({
    '月': pd.date_range('2025-01', periods=12, freq='MS'),
    '売上': [950, 1020, 880, 1100, 1050, 980, 1150, 1200, 1080, 1300, 1250, 1400],
    '利益': [95, 102, 88, 110, 105, 98, 115, 120, 108, 130, 125, 140],
    'カテゴリ': ['低','中','低','高','高','中','高','高','中','高','高','高']
})

# 簡単なインタラクティブグラフ
fig = px.line(df, x='月', y='売上', title='月次売上推移',
              markers=True, hover_data=['利益'])
fig.update_layout(hovermode='x unified')
fig.write_html('revenue_chart.html')  # HTMLとして保存(ブラウザで開ける)

# 複合グラフ(棒グラフ+折れ線グラフ)
fig2 = make_subplots(specs=[[{'secondary_y': True}]])
fig2.add_trace(go.Bar(x=df['月'], y=df['売上'], name='売上'), secondary_y=False)
fig2.add_trace(go.Scatter(x=df['月'], y=df['利益'], name='利益', mode='lines+markers'), secondary_y=True)
fig2.update_layout(title='売上・利益の推移', hovermode='x unified')
fig2.show()

ヒートマップで相関を可視化

import seaborn as sns

# 相関マトリックスのヒートマップ
corr = df[['売上', '利益']].corr()
plt.figure(figsize=(8, 6))
sns.heatmap(corr, annot=True, fmt='.2f', cmap='coolwarm',
            vmin=-1, vmax=1, center=0,
            square=True, linewidths=0.5)
plt.title('特徴量の相関マトリックス')
plt.tight_layout()
plt.show()

まとめ

データ可視化は「誰に見せるか」で選ぶツールが変わります。エンジニア向けの分析にはmatplotlib・seaborn、現場・経営層への報告にはplotlyのインタラクティブグラフ、ダッシュボードにはStreamlit・Dashが適しています。まずはmatplotlibで基本を身につけて、plotlyでインタラクティブ化を試してみましょう。

💼 ITエンジニア転職特化

自分らしく働けるエンジニア転職を目指すなら【strategy career】

年収1000万・残業月30時間以下・リモート可の求人多数

💼 無料で転職相談する →

※アフィリエイト広告を含みます

※本記事にはアフィリエイトリンクが含まれます。

コメント

タイトルとURLをコピーしました