اذهب إلى المحتوى

السؤال

نشر

السلام عليكم

plt.figure(figsize=(12,8))

plt.subplot(2,2,1)

plt.subplot(2,2,2)

sns.kdeplot(data_train_demographics['adult_child'], fill=True, color='skyblue', bw_adjust=0.5)
plt.title("KDE Plot of Age")           
plt.xlabel("Age")                      


plt.subplot(2,2,2)

sns.histplot(data_train_demographics, kde=False)
plt.title('Distrubution of HLA Match DQB1 High')
plt.xlabel("HLA Match DQB1 High")
plt.ylabel("Count")

plt.subplot(2,2,3)

sns.countplot(data=data_train , x='tce_imm_match' , color='blue')
plt.title('Distrubution of TCE IMM Match')
plt.xlabel("ICE IMM Match")
plt.ylabel("Count")

plt.subplot(2,2,4)

sns.countplot(data=data_train , x='hla_nmdp_6' , color='blue')
plt.title('Distrubution of HLA NMDP 6')
plt.xlabel("HLA NMDP 6")
plt.ylabel("Count")

plt.tight_layout()
plt.show()

 

Recommended Posts

  • 0
نشر

وعليكم السلام ورحمة الله وبركاته.

لاحظ أنك أولا قمت باستخدام plt.subplot(2,2,2) مرتين بدون رسم في المرة الأولى وهذا سيؤدي إلى مشكلة بسبب التعارض.

يمكنك إذا أردت وضع الكود بأكمله في دالة وذلك منعا للتكرار لو تقوم بتنفيذ الكود أكثر من مرة :

import matplotlib.pyplot as plt
import seaborn as sns

def plot_demographics(data_train, data_train_demographics):
    plt.figure(figsize=(12, 8))

    # الرسم البياني الأول: KDE Plot
    plt.subplot(2, 2, 1)
    sns.kdeplot(data_train_demographics['adult_child'], fill=True, color='skyblue', bw_adjust=0.5)
    plt.title("KDE Plot of Age")
    plt.xlabel("Age")

    # الرسم البياني الثاني: Histogram
    plt.subplot(2, 2, 2)
    sns.histplot(data_train_demographics['hla_match_dqb1_high'], kde=False, color='skyblue')
    plt.title('Distribution of HLA Match DQB1 High')
    plt.xlabel("HLA Match DQB1 High")
    plt.ylabel("Count")

    # الرسم البياني الثالث: Count Plot ل TCE IMM Match
    plt.subplot(2, 2, 3)
    sns.countplot(data=data_train, x='tce_imm_match', color='blue')
    plt.title('Distribution of TCE IMM Match')
    plt.xlabel("TCE IMM Match")
    plt.ylabel("Count")

    # الرسم البياني الرابع: Count Plot ل HLA NMDP 6
    plt.subplot(2, 2, 4)
    sns.countplot(data=data_train, x='hla_nmdp_6', color='blue')
    plt.title('Distribution of HLA NMDP 6')
    plt.xlabel("HLA NMDP 6")
    plt.ylabel("Count")

    plt.tight_layout()
    plt.show()

# استدعاء الدالة و تمرير البيانات لها
plot_demographics(data_train, data_train_demographics)

أما لو أردت إستعمال OOP حيث تجعل لكل رسم دالة خاصة به وذلك لرسمه دون تكرار للأكواد يمكنك إستخدام التالي :

import matplotlib.pyplot as plt
import seaborn as sns

class DemographicVisualizer:
    def __init__(self, data_train, data_train_demographics, figsize=(12, 8)):
        self.data_train = data_train
        self.data_train_demographics = data_train_demographics
        self.figsize = figsize
        self.colors = {'kde': 'skyblue', 'count': 'blue'}

    def plot_kde(self, ax, column, title, xlabel):
        sns.kdeplot(self.data_train_demographics[column], fill=True, color=self.colors['kde'], bw_adjust=0.5, ax=ax)
        ax.set_title(title)
        ax.set_xlabel(xlabel)

    def plot_histogram(self, ax, column, title, xlabel, ylabel):
        sns.histplot(self.data_train_demographics[column], kde=False, color=self.colors['kde'], ax=ax)
        ax.set_title(title)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)

    def plot_count(self, ax, column, title, xlabel, ylabel, data_source='data_train'):
        data = self.data_train if data_source == 'data_train' else self.data_train_demographics
        sns.countplot(data=data, x=column, color=self.colors['count'], ax=ax)
        ax.set_title(title)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)

    def plot_all(self):
        fig, axes = plt.subplots(2, 2, figsize=self.figsize)

        # الرسم البياني الأول: KDE Plot
        self.plot_kde(axes[0, 0], 'adult_child', 'KDE Plot of Age', 'Age')

        # الرسم البياني الثاني: Histogram
        self.plot_histogram(axes[0, 1], 'hla_match_dqb1_high', 'Distribution of HLA Match DQB1 High', 
                           'HLA Match DQB1 High', 'Count')

        # الرسم البياني الثالث: Count Plot ل TCE IMM Match
        self.plot_count(axes[1, 0], 'tce_imm_match', 'Distribution of TCE IMM Match', 
                       'TCE IMM Match', 'Count', data_source='data_train')

        # الرسم البياني الرابع: Count Plot ل HLA NMDP 6
        self.plot_count(axes[1, 1], 'hla_nmdp_6', 'Distribution of HLA NMDP 6', 
                       'HLA NMDP 6', 'Count', data_source='data_train')

        plt.tight_layout()
        plt.show()

# استخدام الكائن
visualizer = DemographicVisualizer(data_train, data_train_demographics)
visualizer.plot_all()

وهكذا وضعنا كل رسم في دالة داخل الصنف حيث يمكنك إستخدامها وطباعتها بشكل منفصل . وأيضا قمنا بتجميع طباعة جميع الرسومات في الدالة plot_all وبمجرد إستدعائها يصبح الكود كما الكود الذي أرفقته أنت.

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...