Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment 8

Aim: To explore different libraries for visualisation in Python and R.


Libraries:
Python

1. Matplotlib:
Description: Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python. It provides a MATLAB-like interface for creating a wide range of plots.

import matplotlib.pyplot as plt

# Simple line plot

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('Simple Plot')

plt.show()

2. Seaborn:
Description: Seaborn is built on top of Matplotlib and provides a high-level interface for creating
attractive statistical graphics. It simplifies the process of visualizing complex relationships in data.

import seaborn as sns

import matplotlib.pyplot as plt

# Scatter plot with regression line

sns.regplot(x=[1, 2, 3, 4], y=[1, 4, 9, 16])

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('Scatter Plot with Regression Line')

plt.show()

3. Plotly:
Description: Plotly is a versatile library for creating interactive and publication-quality plots. It
supports a wide range of chart types and can be used to build interactive dashboards.

import plotly.graph_objects as go
# Scatter plot
fig = go.Figure(data=go.Scatter(x=[1, 2, 3, 4], y=[1, 4, 9, 16]))

fig.update_layout(title='Scatter Plot', xaxis_title='X-axis',


yaxis_title='Y-axis')

fig.show()

4. Bokeh:
Description: Bokeh is designed for creating interactive, web-ready plots. It provides tools for building
interactive apps and dashboards with complex visualizations.

from bokeh.plotting import figure, show

# Line plot

p = figure(title='Line Plot', x_axis_label='X-axis',


y_axis_label='Y-axis')

p.line(x=[1, 2, 3, 4], y=[1, 4, 9, 16])

show(p)

5. ggplot (ggpy):
Description: ggplot is a Python implementation of the popular ggplot2 library in R. It follows the
Grammar of Graphics principles, making it easy to create complex visualizations.

from ggplot import ggplot, aes, geom_point

# Scatter plot

p = ggplot(aes(x='x', y='y'), data={'x': [1, 2, 3, 4], 'y': [1, 4,


9, 16]}) + geom_point()

print(p)

6. Altair:
Description: Altair is a declarative statistical visualization library based on the Vega and Vega-Lite
visualization grammars. It allows for concise and expressive visualization code.

import altair as alt

import pandas as pd

# Scatter plot

df = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [1, 4, 9, 16]})


chart = alt.Chart(df).mark_point().encode(x='x', y='y')

chart.show()

7. Plotnine:
Description: Plotnine is another Python implementation of ggplot2, focusing on creating grammar of
graphics-style plots. It provides a high-level API for creating complex visualizations.

from plotnine import ggplot, aes, geom_point

# Scatter plot

p = ggplot(aes(x='x', y='y'), data=pd.DataFrame({'x': [1, 2, 3, 4],


'y': [1, 4, 9, 16]})) + geom_point()

print(p)

8. Pygal:
Description: Pygal is a Python library for creating SVG (Scalable Vector Graphics) plots. It's simple
to use and produces attractive vector-based graphics suitable for web applications.

import pygal

# Line plot

line_chart = pygal.Line()

line_chart.add('Line', [1, 4, 9, 16])

line_chart.render_in_browser()
R

1. ggplot2 (Most Popular):


This is arguably the most widely used R visualization library due to its grammar-based approach and
ease of customization.

# Sample data

data <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 5, 7, 2, 4))

# Basic scatter plot

ggplot(data, aes(x = x, y = y)) +

geom_point() +

labs(title = "Scatter Plot", x = "X-axis", y = “Y-axis")


2. ggplot2 (More Plots):
ggplots strength lies in its versatility. You can use the same basic structure to create various plots by
changing the geom type.
Line Plot:

ggplot(data, aes(x = x, y = y)) +

geom_line()

Histogram:

ggplot(data, aes(x = y)) +

geom_histogram()
Bar Chart:

ggplot(data, aes(x = x, y = y)) +

geom_bar(stat = “identity")

3. plotly (Interactive Plots):


Plotly allows you to create interactive visualizations that can be embedded in web pages or
dashboards.

# Sample data (time series)

data <- data.frame(date = seq(as.Date("2023-01-01"), by = "days",


length = 10),

value = rnorm(10, mean = 50, sd = 10))

# Interactive line plot

plot_ly(data, x = ~date, y = ~value, type = 'scatter') %>%

layout(title = "Line Plot”)

4. Other Libraries
Lattice: Offers high-level trellis graphics for visualizing complex data relationships.
highcharter: Creates interactive charts based on the popular Highcharts library.
Leaflet: Useful for creating interactive maps.

Conclusion: Thus, we have explored different visualisation libraries in Python and R.

You might also like