Data Visualization

import matplotlib.pyplot as plt
import seaborn as sns

1. Line Chart

plt.figure(figsize=(16,6))  # Set the width and height of the figure
plt.title("title") # Add title
sns.lineplot(data=fifa_data) # Line chart
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You") #Line chart showing specified data
plt.xlabel("Date") # Add label for horizontal axis

2. Bar Charts、Heatmaps、Scatter

sns.barplot(x=flight_data.index, y=flight_data['NK'])
sns.heatmap(data=flight_data, annot=True)
sns.scatterplot(x=insurance_data['bmi'], y=insurance_data['charges'])
sns.scatterplot(x=insurance_data['bmi'], y=insurance_data['charges'], hue=insurance_data['smoker'])# color the points by smoker
sns.regplot  #regression line in the scatter plot
sns.lmplot(x="bmi", y="charges", hue="smoker", data=insurance_data)  #add two regression lines
sns.swarmplot(x=insurance_data['smoker'],y=insurance_data['charges']) #categorical scatter plot

3. Distributions

sns.histplot(iris_data['Petal Length (cm)']) #show the distributes and frequency
sns.histplot(data=iris_data, x='Petal Length (cm)', hue='Species') # Histograms for each species
sns.kdeplot(data=iris_data['Petal Length (cm)'], shade=True) #kernel density estimate (smoothed histogram)
sns.kdeplot(data=iris_data, x='Petal Length (cm)', hue='Species', shade=True)  # KDE for each species
sns.jointplot(x=iris_data['Petal Length (cm)'], y=iris_data['Sepal Width (cm)'], kind="kde") #2D KDE plots

4. Matplotlib

x = np.linspace(0, 10, 100) #100 data points range from 1-10
fig = plt.figure()
plt.plot(x, np.sin(x), '-') #drwa a sinx curve
plt.plot(x, np.cos(x), '--'); #drwa a cosx curve
plt.subplot(2, 1, 1)   # (rows, columns, panel number)
plt.plot([1, 2, 3, 4]) # the default x data are [0,1,2,3] 
plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) # plot x versus y
plt.plot(x, x, 'r--', x, x**2, 'bs', x, x**3, 'g^') # red dashes, blue squares and green triangles
#plot at the same figure
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
plt.legend() # show the label
plt.show() # show the figure

plt.xlim([1.0, 4.0]) 
plt.xticks([2, 4, 6, 8, 10])
plt.ylabel('Numbers') #vertical label
plt.title('First Plot')

#Object-Oriented API
fig, ax = plt.subplots(2) # ax will be an array of two Axes objects
ax[0].plot(x1, np.sin(x), 'b-')
ax[1].plot(x1, np.cos(x), 'b-');
posted @ 2022-06-07 00:04  失控D大白兔  阅读(156)  评论(0编辑  收藏  举报