Data Visualizations

Dataviz library

  • Pandas-Profiling

  • Sweetviz

  • Autoviz

  • D-Tale

Subplot syntax

To plot 3 x 2 plots

variables = ["Age", "Education", "Usage", "Fitness", "Income", "Miles"]

fig, axes = plt.subplots(2, 3, figsize=(20, 12))

for i in range(2):
    for j in range(3):
        variable = variables[i * 3 + j]
        sns.boxplot(ax=axes[i, j], data=df, x="Product", y=variable, hue="Gender")
        axes[i, j].set_title(f"Gender wise {variable} vs Product")

plt.show();

Subplots using single line

data.plot(kind='density', subplots=True, layout=(3, 3), sharex=False)

To generate box plot with solid background color

colors = dict(boxes='lightblue', whiskers='dimgrey', medians='dimgrey', caps='dimgrey')
df.plot(kind='box', subplots=True, layout=(3, 4), sharex=False, figsize=(12, 15), patch_artist=True, color=colors);
plt.suptitle("Outliers", y=0.92, fontsize=14);

Add a Pie chart inside the subplot

unique_users["Gender"].value_counts(normalize=True )[:10].plot(kind="pie", autopct='%1.1f%%', startangle=90, ax=axes[1,0])
axes[1,0].set_title("Gender Distribution");

Load top n values in seaborn countplot

sns.countplot(x="Product_Category", data=df,
    order=df["Product_Category"].value_counts().iloc[:10].index)

Plot a Gantt Chart using Plotly

symbolbuy_datesell_datebuy_pricesell_pricequantitydays_diffprofit

1510

PERSISTENT

2023-04-25

2023-06-09

4448.000000

4860.000000

2.0

7 days

824.0

367

BEL

2020-05-28

2020-07-29

21.390625

29.890625

467.0

16 days

3969.5

1593

POONAWALLA

2022-06-10

2022-06-13

247.750000

230.625000

40.0

29 days

-685.0

493

CIPLA

2021-11-24

2021-11-25

882.000000

888.500000

11.0

2 days

71.5

65

ADANIENT

2022-05-31

2022-06-01

2166.000000

2148.000000

4.0

5 days

-72.0

import plotly.express as px

fig = px.timeline(dataframe, x_start="buy_date", x_end="sell_date", y="symbol", color="symbol")
fig.update_layout(xaxis_rangeslider_visible=True)
fig.show()

Plot Line equation using matplotlib

import numpy as np
import matplotlib.pyplot as plt

x_values = np.linspace(-10, 10, 100)  

y_values = 3 * x_values + 4

# Plot the graph
plt.plot(x_values, y_values, label='y = 3x + 4')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of y = 3x + 4')
plt.grid(True)
plt.legend()
plt.show()

Last updated