Ask AI
Code Examples
Example 0:
- import numpy as np
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- from mpl_toolkits.mplot3d import proj3d
- fig = plt.figure(figsize=(10,10)) #Define figure size
- ax = fig.add_subplot(111, projection='3d')
- plt.rcParams['legend.fontsize'] = 20
- np.random.seed(4294967294) #Used the random seed for consistency
- mu_vec_1 = np.array([0,0,0])
- cov_mat_1 = np.array([[1,0,0],[0,1,0],[0,0,1]])
- class_1_sample = np.random.multivariate_normal(mu_vec_1, cov_mat_1, 30).T
- assert class_1_sample.shape == (3,30), "The matrix dimensions is not 3x30"
- mu_vec_2 = np.array([1,1,1])
- cov_mat_2 = np.array([[1,0,0],[0,1,0],[0,0,1]])
- class_2_sample = np.random.multivariate_normal(mu_vec_2, cov_mat_2, 30).T
- assert class_2_sample.shape == (3,30), "The matrix dimensions is not 3x30"
- ax.plot(class_1_sample[0,:], class_1_sample[1,:], class_1_sample[2,:], 'o', markersize=10, color='green', alpha=1.0, label='Class 1')
- ax.plot(class_2_sample[0,:], class_2_sample[1,:], class_2_sample[2,:], 'o', markersize=10, alpha=1.0, color='red', label='Class 2')
- plt.title('Data Samples for Classes 1 & 2', y=1.04)
- ax.legend(loc='upper right')
- plt.savefig('Multivariate_distr.png', bbox_inches='tight')
- plt.show()
Example one:
- import matplotlib.pyplot as plt
- import seaborn as sns
- # Load Dataset
- df = sns.load_dataset('iris')
- # Plot
- plt.figure(figsize=(10,8), dpi= 80)
- sns.pairplot(df, kind="reg", hue="species")
- plt.show()
- import warnings
- warnings.filterwarnings('ignore')
- import seaborn as sns
- import matplotlib.pyplot as plt
- import pandas as pd
- sns.set()
- iris = sns.load_dataset("iris")
- sns.pairplot(iris, hue='species', height=2.5)
- textstr = 'Created at www.tssfl.com'
- plt.gcf().text(0.36, 0.985, textstr, fontsize=14, color='green')
- plt.show()
- import seaborn as sns
- import matplotlib.pyplot as plt
- import numpy as np
- import pandas as pd
- df = pd.DataFrame(np.random.randn(250, 4), columns=[f"Label%s{i+1}" % " " for i in range(4)])
- df["Choice"] = np.random.choice([1., 0.], size=len(df))
- sns.pairplot(df, vars=df.columns[:-1], hue="Choice")
- plt.show()
Example Two:
- import matplotlib.pyplot as plt
- names = ['group_a', 'group_b', 'group_c']
- values = [1, 10, 100]
- plt.figure(figsize=(9, 3))
- plt.subplot(131)
- plt.bar(names, values)
- plt.subplot(132)
- plt.scatter(names, values)
- plt.subplot(133)
- plt.plot(names, values)
- plt.suptitle('Categorical Plotting')
- plt.show()
Example Three:
- import matplotlib.pyplot as plt
- import pandas as pd
- import seaborn as sns
- # Import Dataset
- df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mtcars.csv")
- # Plot
- plt.figure(figsize=(12,10), dpi= 80)
- sns.heatmap(df.corr(), xticklabels=df.corr().columns, yticklabels=df.corr().columns, cmap='RdYlGn', center=0, annot=True)
- textstr = 'Created at www.tssfl.com'
- plt.gcf().text(0.64, 0.893, textstr, fontsize=14, color='darkblue')
- # Decorations
- plt.title('Correlogram of mtcars', fontsize=22, y=1)
- plt.xticks(fontsize=12)
- plt.yticks(fontsize=12)
- plt.show()
Example Four:
- import matplotlib.pyplot as plt
- import numpy as np
- import pandas as pd
- # Import Data
- df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")
- # Prepare data
- x_var = 'displ'
- groupby_var = 'class'
- df_agg = df.loc[:, [x_var, groupby_var]].groupby(groupby_var)
- vals = [df[x_var].values.tolist() for i, df in df_agg]
- # Draw
- plt.figure(figsize=(16,9), dpi= 80)
- colors = [plt.cm.Spectral(i/float(len(vals)-1)) for i in range(len(vals))]
- n, bins, patches = plt.hist(vals, 30, stacked=True, density=False, color=colors[:len(vals)])
- # Decoration
- plt.legend({group:col for group, col in zip(np.unique(df[groupby_var]).tolist(), colors[:len(vals)])})
- plt.title(f"Stacked Histogram of ${x_var}$ colored by ${groupby_var}$", fontsize=22)
- plt.xlabel(x_var)
- plt.ylabel("Frequency")
- plt.ylim(0, 25)
- #plt.xticks(ticks=bins[::3], labels=[round(b,1) for b in bins[::3]])
- plt.show()
Output for Example One: