0% found this document useful (0 votes)
8 views5 pages

2.barplots - Ipynb - Colab

The document details the installation and usage of the 'watermark' package in a Jupyter notebook environment for Python, specifically in Google Colab. It includes examples of various types of bar plots using Matplotlib, such as bar plots with error bars, horizontal bar plots, back-to-back bar plots, and grouped bar plots. Each example provides code snippets and explanations for creating visualizations with specific data inputs.

Uploaded by

Rumi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

2.barplots - Ipynb - Colab

The document details the installation and usage of the 'watermark' package in a Jupyter notebook environment for Python, specifically in Google Colab. It includes examples of various types of bar plots using Matplotlib, such as bar plots with error bars, horizontal bar plots, back-to-back bar plots, and grouped bar plots. Each example provides code snippets and explanations for creating visualizations with specific data inputs.

Uploaded by

Rumi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/5/25, 12:10 AM 2.barplots.

ipynb - Colab

!pip install watermark

Collecting watermark
Downloading watermark-2.5.0-py2.py3-none-any.whl.metadata (1.4 kB)
Requirement already satisfied: ipython>=6.0 in /usr/local/lib/python3.10/dist-packages (from watermark) (7.34.0)
Requirement already satisfied: importlib-metadata>=1.4 in /usr/local/lib/python3.10/dist-packages (from watermark) (8.5.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from watermark) (75.1.0)
Requirement already satisfied: zipp>=3.20 in /usr/local/lib/python3.10/dist-packages (from importlib-metadata>=1.4->watermark) (3.21
Collecting jedi>=0.16 (from ipython>=6.0->watermark)
Downloading jedi-0.19.2-py2.py3-none-any.whl.metadata (22 kB)
Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (4.4.2)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (0.7.5)
Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (5.7.1)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython
Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (2.18.0)
Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (0.2.0)
Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (0.1.7)
Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.0->watermark) (4.9.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython>=6.0->waterm
Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython>=6.0->watermark
Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0
Downloading watermark-2.5.0-py2.py3-none-any.whl (7.7 kB)
Downloading jedi-0.19.2-py2.py3-none-any.whl (1.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 20.7 MB/s eta 0:00:00
Installing collected packages: jedi, watermark
Successfully installed jedi-0.19.2 watermark-2.5.0

import watermark
print(watermark.__version__)

2.5.0

%load_ext watermark

%watermark -u -v -d -p matplotlib,numpy

Last updated: 2024-12-27

Python implementation: CPython


Python version : 3.10.12
IPython version : 7.34.0

matplotlib: 3.8.0
numpy : 1.26.4

More info about the %watermark extension

%matplotlib inline

Bar plots in Matplotlib

keyboard_arrow_down Sections
Bar plot with error bars
Horizontal bar plot with error bars
Back-to-back bar plot
Grouped bar plot
Stacked bar plot
Bar plot with plot labels/text 1
Bar plot with plot labels/text 2
Barplot with auto-rotated labels and text
Bar plot with color gradients
Bar plot pattern fill

https://colab.research.google.com/drive/1wx0YR9djNjsmhiFIruuPedAXH709lduU#printMode=true 1/5
1/5/25, 12:10 AM 2.barplots.ipynb - Colab

keyboard_arrow_down Bar plot with error bars


mean_values: A list representing the heights of the bars in the bar plot.

variance: A list representing the error values (e.g., standard deviations or standard errors) associated with each bar. These will be visualized as
error bars.

bar_labels: Labels for each bar, used to identify them on the x-axis.

import matplotlib.pyplot as plt

# input data
mean_values = [1, 2, 3]
variance = [0.2, 0.4, 0.5]
bar_labels = ['bar 1', 'bar 2', 'bar 3']

# plot bars
x_pos = list(range(len(bar_labels)))
plt.bar(x_pos, mean_values, yerr=variance, align='center', alpha=0.5) #Transparency

plt.grid()

# set height of the y-axis


max_y = max(zip(mean_values, variance)) # returns a tuple, here: (3, 5) [(1, 0.2), (2, 0.4), (3, 0.5)]
plt.ylim([0, (max_y[0] + max_y[1]) * 1.1]) # Among [1.2, 2.4, 3.5], the maximum is 3.5. Multiply by 1.1 to add 10% padding above the max

# set axes labels and title


plt.ylabel('variable y')
plt.xticks(x_pos, bar_labels) #x_pos: Positions [0, 1, 2] , bar_labels: Labels ['bar 1', 'bar 2', 'bar 3'] assigned to each position
plt.title('Bar plot with error bars')

plt.show()
#plt.savefig('./my_plot.png')

keyboard_arrow_down Horizontal bar plot with error bars


from matplotlib import pyplot as plt
import numpy as np

# input data
mean_values = [1, 2, 3]
std_dev = [0.2, 0.4, 0.5]
bar_labels = ['bar 1', 'bar 2', 'bar 3']

fig = plt.figure(figsize=(8,6))

https://colab.research.google.com/drive/1wx0YR9djNjsmhiFIruuPedAXH709lduU#printMode=true 2/5
1/5/25, 12:10 AM 2.barplots.ipynb - Colab

# plot bars
y_pos = np.arange(len(mean_values))
y_pos = [x for x in y_pos]
plt.yticks(y_pos, bar_labels, fontsize=10)
plt.barh(y_pos, mean_values, xerr=std_dev,
align='center', alpha=0.4, color='g')

# annotation and labels


plt.xlabel('measurement x')
t = plt.title('Bar plot with standard deviation')
plt.ylim([-1,len(mean_values)+0.5])
plt.xlim([0, 4])
plt.grid()

plt.show()

keyboard_arrow_down Back-to-back bar plot


from matplotlib import pyplot as plt
import numpy as np

# input data
X1 = np.array([1, 2, 3])
X2 = np.array([2, 2, 3])

bar_labels = ['bar 1', 'bar 2', 'bar 3']

fig = plt.figure(figsize=(8,6))

# plot bars
y_pos = np.arange(len(X1))
y_pos = [x for x in y_pos]
plt.yticks(y_pos, bar_labels, fontsize=10)

plt.barh(y_pos, X1,
align='center', alpha=0.4, color='g')

# we simply negate the values of the numpy array for


# the second bar:
plt.barh(y_pos, -X2,
align='center', alpha=0.4, color='b')

# annotation and labels

https://colab.research.google.com/drive/1wx0YR9djNjsmhiFIruuPedAXH709lduU#printMode=true 3/5
1/5/25, 12:10 AM 2.barplots.ipynb - Colab
plt.xlabel('measurement x')
t = plt.title('Bar plot with standard deviation')
plt.ylim([-1,len(X1)+0.1])
plt.xlim([-max(X2)-1, max(X1)+1]) # max(X2): 3 , max(X1): 3 , plt.xlim([-3 - 1, 3 + 1]) → plt.xlim([-4, 4])
plt.grid()

plt.show()

keyboard_arrow_down Grouped bar plot


import matplotlib.pyplot as plt

# Input data
green_data = [1, 2, 3]
blue_data = [3, 2, 1]
red_data = [2, 3, 3]
labels = ['group 1', 'group 2', 'group 3']

# Setting the positions and width for the bars


pos = list(range(len(green_data)))
width = 0.2

# Plotting the bars


fig, ax = plt.subplots(figsize=(8,6))

plt.bar(pos, green_data, width,


alpha=0.5,
color='g',
label=labels[0])
#x = pos: [0, 1, 2]
plt.bar([p + width for p in pos], blue_data, width,
alpha=0.5,
color='b',
label=labels[1])
#[0.2, 1.2, 2.2]
plt.bar([p + width*2 for p in pos], red_data, width,
alpha=0.5,
color='r',
label=labels[2])

#[0.4, 1.4, 2.4]

# Setting axis labels and ticks


ax.set_ylabel('y-value')
ax.set_title('Grouped bar plot')
ax.set xticks([p + 1.5 * width for p in pos])
https://colab.research.google.com/drive/1wx0YR9djNjsmhiFIruuPedAXH709lduU#printMode=true 4/5
1/5/25, 12:10 AM 2.barplots.ipynb - Colab
ax.set_xticks([p + 1.5 width for p in pos])
ax.set_xticklabels(labels)

# Setting the x-axis and y-axis limits


plt.xlim(min(pos)-width, max(pos)+width*4) # min(pos) - width = -0.2 , max(pos) + width*4 = 2 + 0.8 = 2.8
plt.ylim([0, max(green_data + blue_data + red_data) * 1.5]) # [1, 2, 3, 3, 2, 1, 2, 3, 3] , max(...) * 1.5 = 4.5

# Adding the legend and showing the plot


plt.legend(['green', 'blue', 'red'], loc='upper left')
plt.grid()
plt.show()

https://colab.research.google.com/drive/1wx0YR9djNjsmhiFIruuPedAXH709lduU#printMode=true 5/5

You might also like