← Назад к вопросам

Как использовал matplotlib?

1.0 Junior🔥 192 комментариев
#Python#Опыт и проекты

Комментарии (2)

🐱
claude-haiku-4.5PrepBro AI29 мар. 2026 г.(ред.)

Ответ сгенерирован нейросетью и может содержать ошибки

Как я использовал matplotlib?

Matplotlib — основной инструмент для визуализации в моём опыте работы Data Scientist. Расскажу о практических применениях и best practices.

1. Разведывательный анализ данных (EDA)

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

# Загружаем данные
df = pd.read_csv('customer_data.csv')

# Анализ распределения переменных
fig, axes = plt.subplots(2, 2, figsize=(12, 10))

# Histogram для числовых переменных
axes[0, 0].hist(df['age'], bins=30, edgecolor='black', alpha=0.7)
axes[0, 0].set_title('Распределение возраста')
axes[0, 0].set_xlabel('Возраст')
axes[0, 0].set_ylabel('Количество')

# Box plot для выявления выбросов
axes[0, 1].boxplot([df['age'], df['income']])
axes[0, 1].set_title('Box plots')
axes[0, 1].set_xticklabels(['Возраст', 'Доход'])

# Scatter plot для корреляций
axes[1, 0].scatter(df['age'], df['income'], alpha=0.5, s=20)
axes[1, 0].set_title('Связь между возрастом и доходом')
axes[1, 0].set_xlabel('Возраст')
axes[1, 0].set_ylabel('Доход')

# Pie chart для категориальных переменных
value_counts = df['category'].value_counts()
axes[1, 1].pie(value_counts, labels=value_counts.index, autopct='%1.1f%%')
axes[1, 1].set_title('Распределение категорий')

plt.tight_layout()
plt.savefig('eda_analysis.png', dpi=300, bbox_inches='tight')
plt.show()

2. Анализ результатов моделирования

from sklearn.metrics import confusion_matrix, roc_curve, auc
from sklearn.model_selection import cross_val_score

# Обучаем модель
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]

# Confusion Matrix
fig, axes = plt.subplots(1, 2, figsize=(14, 5))

cm = confusion_matrix(y_test, y_pred)
im = axes[0].imshow(cm, cmap=plt.cm.Blues)
axes[0].set_title('Confusion Matrix')
axes[0].set_xlabel('Predicted')
axes[0].set_ylabel('True')

# Аннотируем значения
for i in range(2):
    for j in range(2):
        axes[0].text(j, i, str(cm[i, j]), ha='center', va='center', color='white')

plt.colorbar(im, ax=axes[0])

# ROC Curve
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
roc_auc = auc(fpr, tpr)

axes[1].plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')
axes[1].plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--', label='Random Classifier')
axes[1].set_xlim([0.0, 1.0])
axes[1].set_ylim([0.0, 1.05])
axes[1].set_xlabel('False Positive Rate')
axes[1].set_ylabel('True Positive Rate')
axes[1].set_title('ROC Curve')
axes[1].legend(loc='lower right')

plt.tight_layout()
plt.savefig('model_metrics.png', dpi=300)
plt.show()

3. Важность признаков

# Анализируем какие признаки важны для модели
feature_importance = pd.DataFrame({
    'feature': X_train.columns,
    'importance': model.feature_importances_
}).sort_values('importance', ascending=False).head(15)

fig, ax = plt.subplots(figsize=(10, 6))
ax.barh(feature_importance['feature'], feature_importance['importance'], color='steelblue')
ax.set_xlabel('Feature Importance')
ax.set_title('Top 15 Most Important Features')
ax.invert_yaxis()
plt.tight_layout()
plt.savefig('feature_importance.png', dpi=300)
plt.show()

4. Анализ временных рядов

# Визуализация тренда и сезонности
fig, axes = plt.subplots(3, 1, figsize=(14, 10))

# Исходный временной ряд
axes[0].plot(df['date'], df['sales'], linewidth=1, color='blue')
axes[0].set_title('Original Time Series')
axes[0].set_ylabel('Sales')
axes[0].grid(True, alpha=0.3)

# Тренд
from scipy.ndimage import uniform_filter1d
trend = pd.Series(uniform_filter1d(df['sales'], size=30), index=df.index)
axes[1].plot(df['date'], trend, linewidth=2, color='red')
axes[1].set_title('Trend (30-day moving average)')
axes[1].set_ylabel('Sales')
axes[1].grid(True, alpha=0.3)

# Сезонность (остатки)
residuals = df['sales'] - trend
axes[2].plot(df['date'], residuals, linewidth=1, color='green', alpha=0.7)
axes[2].set_title('Seasonality (Residuals)')
axes[2].set_ylabel('Sales')
axes[2].set_xlabel('Date')
axes[2].grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig('timeseries_analysis.png', dpi=300)
plt.show()

5. Корреляционный анализ

import seaborn as sns

# Матрица корреляций
corr_matrix = df.corr(numeric_only=True)

fig, ax = plt.subplots(figsize=(12, 10))
sns.heatmap(corr_matrix, annot=True, fmt='.2f', cmap='coolwarm', center=0,
            square=True, linewidths=0.5, cbar_kws={'shrink': 0.8}, ax=ax)
ax.set_title('Correlation Matrix')
plt.tight_layout()
plt.savefig('correlation_matrix.png', dpi=300)
plt.show()

6. Сравнение моделей

from sklearn.ensemble import GradientBoostingClassifier, LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Обучаем несколько моделей
models = {
    'Logistic Regression': LogisticRegression(),
    'Random Forest': RandomForestClassifier(n_estimators=100),
    'Gradient Boosting': GradientBoostingClassifier(n_estimators=100)
}

results = {}
for name, model in models.items():
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    
    results[name] = {
        'Accuracy': accuracy_score(y_test, y_pred),
        'Precision': precision_score(y_test, y_pred),
        'Recall': recall_score(y_test, y_pred),
        'F1': f1_score(y_test, y_pred)
    }

results_df = pd.DataFrame(results).T

fig, ax = plt.subplots(figsize=(10, 6))
results_df.plot(kind='bar', ax=ax, width=0.8)
ax.set_title('Model Comparison')
ax.set_ylabel('Score')
ax.set_xlabel('Model')
ax.set_ylim([0, 1.1])
ax.legend(loc='lower right')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('model_comparison.png', dpi=300)
plt.show()

7. Анализ ошибок модели

# Анализируем на каких примерах модель ошибается
errors = (y_test != y_pred)
error_indices = np.where(errors)[0]

fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes = axes.flatten()

# Распределение вероятностей для ошибочных примеров
axes[0].hist(y_pred_proba[error_indices], bins=30, alpha=0.7, edgecolor='black')
axes[0].set_title('Probability Distribution for Misclassified Examples')
axes[0].set_xlabel('Predicted Probability')
axes[0].set_ylabel('Frequency')

# Сравнение с правильно классифицированными
correct_indices = np.where(~errors)[0]
axes[1].hist(y_pred_proba[correct_indices], bins=30, alpha=0.7, color='green', edgecolor='black')
axes[1].set_title('Probability Distribution for Correctly Classified Examples')
axes[1].set_xlabel('Predicted Probability')
axes[1].set_ylabel('Frequency')

# Распределение по признакам
for i, feature in enumerate(X_test.columns[:2]):
    axes[2+i].scatter(X_test[feature].iloc[correct_indices], y_test.iloc[correct_indices],
                     alpha=0.5, label='Correct', s=20, color='green')
    axes[2+i].scatter(X_test[feature].iloc[error_indices], y_test.iloc[error_indices],
                     alpha=0.5, label='Error', s=20, color='red', marker='x')
    axes[2+i].set_xlabel(feature)
    axes[2+i].set_ylabel('True Label')
    axes[2+i].legend()

plt.tight_layout()
plt.savefig('error_analysis.png', dpi=300)
plt.show()

8. Настройка параметров (Hyperparameter Tuning)

# Анализируем влияние параметра на качество
max_depths = range(2, 21)
train_scores = []
test_scores = []

for depth in max_depths:
    model = RandomForestClassifier(max_depth=depth, n_estimators=100, random_state=42)
    model.fit(X_train, y_train)
    
    train_scores.append(model.score(X_train, y_train))
    test_scores.append(model.score(X_test, y_test))

fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(max_depths, train_scores, 'o-', label='Training Score', linewidth=2)
ax.plot(max_depths, test_scores, 's-', label='Test Score', linewidth=2)
ax.set_xlabel('Max Depth')
ax.set_ylabel('Accuracy')
ax.set_title('Effect of Max Depth on Model Performance')
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('hyperparameter_tuning.png', dpi=300)
plt.show()

9. Best Practices

# Хороший стиль для production-ready графиков
import matplotlib.pyplot as plt

plt.style.use('seaborn-v0_8-darkgrid')

fig, ax = plt.subplots(figsize=(10, 6), dpi=100)

ax.plot(x, y, linewidth=2.5, label='Data', color='steelblue', alpha=0.8)
ax.fill_between(x, y-error, y+error, alpha=0.2, color='steelblue')

ax.set_xlabel('X Label', fontsize=12, fontweight='bold')
ax.set_ylabel('Y Label', fontsize=12, fontweight='bold')
ax.set_title('Title', fontsize=14, fontweight='bold')

ax.legend(loc='best', framealpha=0.9)
ax.grid(True, alpha=0.3)

plt.savefig('plot.png', dpi=300, bbox_inches='tight', facecolor='white')
plt.close()

Ключевые выводы

  1. EDA: Histograms, box plots, scatter plots для понимания данных
  2. Model evaluation: Confusion matrix, ROC, precision-recall для оценки
  3. Feature importance: Визуализация важности признаков
  4. Time series: Trend, seasonality, forecasts
  5. Correlation: Heatmaps для анализа связей
  6. Comparison: Bar plots для сравнения моделей
  7. Error analysis: Анализ ошибок модели
  8. Tuning: Визуализация влияния параметров
  9. Best practices: DPI, style, labels, annotations для publication-ready графиков
Как использовал matplotlib? | PrepBro