<jupyter_start><jupyter_text>Bar Charts *for qualitative variables*<jupyter_code>import seaborn as sb
import matplotlib.pyplot as plt

base_color = sb.color_palette()[0] # to chose one color for the bars 
cat_order = df[' colomn '].value_counts().index # to rearrange the column depending on the occurance of a value in it from more used to less 
sb.countplot( data = df , x = ' the same colomn ' , color = base_color , order = cat_order ) 
# if the features name are long you can plot it horizontally by assigning the colomn to y instead of x 
# or you can use plt.xticks(rotation = 90) to rotate the feature names by 90 degrees
plt.ylabel('value')  # to change the label name ylabel or even xlabel 
plt.figure(figsize = [10, 5]) 
# to create percentage on the bars to represent it's value 
# add annotations
n_points = df.shape[0]
cat_counts = df['cat_var'].value_counts()
locs, labels = plt.xticks() # get the current tick locations and labels
# loop through each pair of locations and labels
for loc, label in zip(locs, labels):
    # get the text property for the label to get the correct count
    count = cat_counts[label.get_text()]
    pct_string = '{:0.1f}%'.format(100*count/n_points)
    # print the annotation just below the top of the bar
    plt.text(loc, count-8, pct_string, ha = 'center', color = 'w')
# if the data is clean you can use this method  barplot instead of countplot
na_counts = df.isna().sum()
base_color = sb.color_palette()[0]
sb.barplot( X  , Y  , color = base_color) # where x is the col names , y is the value 
plt.figure(figsize = [10, 5])<jupyter_output><empty_output><jupyter_text>Pie ChartsA pie chart is a common univariate plot type that is used to depict relative frequencies for levels of a categorical variable. Frequencies in a pie chart are depicted as wedges drawn on a circle: the larger the angle or area, the more common the categorical value taken.<jupyter_code># code for the pie chart seen above
sorted_counts = df[' feature '].value_counts() # to get the number of values in the feature sorted 
plt.pie( sorted_counts , labels = sorted_counts.index , startangle = 90 , counterclock = False); #wedgeprops = {'width' : 0.4} can add this to plot a donut instead
plt.axis('square') # to plot a symmetric circle where x , y are spaced the same
plt.figure(figsize = [10, 5])<jupyter_output><empty_output><jupyter_text>Histograms for quantitiveA histogram is used to plot the distribution of a numeric variable. It's the quantitative version of the bar chart. However, rather than plot one bar for each unique numeric value, values are grouped into continuous bins, and one bar for each bin is plotted depicting the number<jupyter_code># to plot a histogram in plt 
bin_edges = np.arange( df[' label '].min() , df[' label '].max()+1 , 1 ) # to get a reasonable number and size of bins
plt.hist (data = df , x = ' label ' , bins = bin_edges , rwidth = 0.7) 

'''plt.figure(figsize = [10, 5]) # larger figure size for subplots

# histogram on left, example of too-large bin size
plt.subplot(1, 2, 1) # 1 row, 2 cols, subplot 1
bin_edges = np.arange(0, df['num_var'].max()+4, 4)
plt.hist(data = df, x = 'num_var', bins = bin_edges)

# histogram on right, example of too-small bin size
plt.subplot(1, 2, 2) # 1 row, 2 cols, subplot 2
bin_edges = np.arange(0, df['num_var'].max()+1/4, 1/4)
plt.hist(data = df, x = 'num_var', bins = bin_edges)'''
# to plot a hist in seaborn 
bin_edges = np.arange(df[' label '].min(), df[' label '].max()+1, 1)
sb.distplot( df[' label '] , bins = bin_edges , kde = True , hist_kws = {'alpha' : 1} ) # KDE true to plot a dist curve, alpha for transparency of bars
# to specify axis in seaborn ,, in matplotlib you write the first 2 rows only and instead of plt.hist you use ax.hist
fig = plt.figure()
ax = fig.add_axes( [.125, .125, .775, .755] )
# the first two elements of the list the position of the lower-left hand corner of the Axes , the last two elements specifying the Axes width and height
base_color = sb.color_palette()[0]
sb.countplot(data = df, x = 'cat_var', color = base_color, ax = ax)
# to use subplot and plot more than figure in same row or column 
# example of somewhat too-large bin size
plt.subplot(1, 2, 1) # 1 row of plots , 2 cols or 2 plots , subplot number 1 
plt.hist(data = df, x = 'num_var', rwidth = 0.7)
# this will be plotted in the specified place 

# to scale the data 
plt.xscale('log')<jupyter_output><empty_output><jupyter_text>To use Scatter plot  - 2 quantative -<jupyter_code>plt.scatter(data = df, x = 'num_var1', y = 'num_var2') # in matplotlib

sb.regplot(data = df, x = 'disc_var1', y = 'disc_var2', fit_reg = False, x_jitter = 0.2, y_jitter = 0.2, scatter_kws = {'alpha' : 1/3}) # in seaborn
# wehere jitter is used to create noise around each similar values 
# and alpha is used to create the transparency in the data , the darker the more points lies
# fit_reg is to plot a linear line through the plotted data<jupyter_output><empty_output><jupyter_text>To use Heat maps - 2 quanitative -<jupyter_code>bins_x = np.arange(0.5, 10.5+1, 1) # to set min , max , deviation  for x depending on the min - max of the data USE df.display()
bins_y = np.arange(-0.5, 10.5+1, 1)
h2d= plt.hist2d( data = df, x = 'disc_var1', y = 'disc_var2', bins = [bins_x, bins_y], cmap = 'viridis_r', cmin = 0.5 )
# where cmap for the colors which with this wil lreverse it to make dark equals high number 
# cmin the min number of points in area to be plotted
plt.colorbar() # to show the color bar indicating the density of points

counts = h2d[0] # to store the numbers of points in each col

# loop through the cell counts and add text annotations for each
for i in range(counts.shape[0]):
    for j in range(counts.shape[1]):
        c = counts[i,j]
        if c >= 7: # increase visibility on darkest cells
            plt.text(bins_x[i]+0.5, bins_y[j]+0.5, int(c),
                     ha = 'center', va = 'center', color = 'white')
        elif c > 0:
            plt.text(bins_x[i]+0.5, bins_y[j]+0.5, int(c),
                     ha = 'center', va = 'center', color = 'black')<jupyter_output><empty_output><jupyter_text>To use Violin plot - quantative + qualitative -<jupyter_code>base_color = sb.color_palette()[0] # can be ignored it's just to set the color as 1 only
sb.violinplot(data = df, x = 'cat_var', y = 'num_var', color = base_color , inner = None) 
# can remove inner variable and let the plot have more options like the defoult box plot m or use inner = 'quartile' for simplifying the plot 
# can aslo switch the X and y axis for simpler graph if needed<jupyter_output><empty_output><jupyter_text>To use Box plot - quantative + qualitative -<jupyter_code>base_color = sb.color_palette()[0]
sb.boxplot( data = df, x = 'num_var', y = 'cat_var', color = base_color )<jupyter_output><empty_output><jupyter_text>To use Clustered bar chart - 2 qualitative -<jupyter_code>ax = sb.countplot(data = df, x = 'cat_var1', hue = 'cat_var2')
ax.legend(loc = 8, ncol = 3, framealpha = 1, title = 'cat_var2') # where the num of col = the num of categories in the variable<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10823363828289936, 'fraction_numerical': 0.019000703729767768, 'mean_word_length': 4.082975679542203, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '360490', 'license': 'Unlicense', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2344, 'n_words': 944, 'path': 'Plotting.ipynb', 'repo_name': 'shomerthesec/Machine-Learning-Models', 'size': '7105'}