import pandas as pd
import matplotlib.pyplot as plt
Matplotlib
Creating visualizations is a crucial part of understanding and presenting data in chemistry. Matplotlib, a versatile plotting library in Python, allows for detailed and customized visualizations. This tutorial will guide you through the basics of Matplotlib, focusing on chemistry-related data visualization. By the end, you’ll know how to create line plots, bar charts, and scatter plots, which are commonly used in chemistry for displaying trends, comparisons, and relationships in data.
Setting Up Your Environment
First, ensure you have Matplotlib installed. If not, you can install it using pip:
pip install matplotlib
You’ll also need Pandas for managing your data before plotting:
pip install pandas
Importing Libraries
Start by importing the necessary libraries:
Example Data
Let’s create a DataFrame with some chemistry-related data:
= {
data 'Compound': ['Water', 'Ethanol', 'Acetic Acid', 'Acetone', 'Methanol'],
'Boiling Point (°C)': [100, 78.37, 118.1, 56.05, 64.7],
'Molecular Weight (g/mol)': [18.015, 46.07, 60.052, 58.08, 32.04]
}= pd.DataFrame(data)
df df
Compound | Boiling Point (°C) | Molecular Weight (g/mol) | |
---|---|---|---|
0 | Water | 100.00 | 18.015 |
1 | Ethanol | 78.37 | 46.070 |
2 | Acetic Acid | 118.10 | 60.052 |
3 | Acetone | 56.05 | 58.080 |
4 | Methanol | 64.70 | 32.040 |
Creating a Line Plot
A line plot is useful for visualizing changes in a variable, such as the boiling point or molecular weight of compounds.
=(10, 6)) # Set the figure size
plt.figure(figsize'Compound'], df['Boiling Point (°C)'], marker='o', linestyle='-', color='blue')
plt.plot(df['Boiling Points of Compounds')
plt.title('Compound')
plt.xlabel('Boiling Point (°C)')
plt.ylabel(True)
plt.grid(=45) # Rotate the x-axis labels for better readability
plt.xticks(rotation plt.show()
Creating a Bar Chart
Bar charts are great for comparing values across different categories, like comparing the molecular weights of various compounds.
=(10, 6))
plt.figure(figsize'Compound'], df['Molecular Weight (g/mol)'], color='green')
plt.bar(df['Molecular Weights of Compounds')
plt.title('Compound')
plt.xlabel('Molecular Weight (g/mol)')
plt.ylabel(=45)
plt.xticks(rotation plt.show()
Creating a Scatter Plot
Scatter plots help visualize the relationship between two variables. Let’s plot the relationship between molecular weight and boiling point.
=(10, 6))
plt.figure(figsize'Molecular Weight (g/mol)'], df['Boiling Point (°C)'], color='red')
plt.scatter(df['Relationship Between Molecular Weight and Boiling Point')
plt.title('Molecular Weight (g/mol)')
plt.xlabel('Boiling Point (°C)')
plt.ylabel(True)
plt.grid( plt.show()
Customizing Plots
Matplotlib offers extensive customization options. Here are a few:
- Changing Colors and Markers: You can change the color by setting the color parameter and the marker style with marker.
- Adding a Legend: Use plt.legend() to add a legend if your plot has multiple lines or markers.
- Setting Grid Lines: Enable grid lines with plt.grid(True) for better readability.
- Adjusting Ticks: Use plt.xticks() and plt.yticks() to customize tick marks on the axes.
Saving Plots
You can save your plots to files using plt.savefig()
:
'plot.png') # Saves the last plotted figure plt.savefig(
Subplots
Subplots allow you to display multiple plots in a single figure. This is useful for comparing different datasets or aspects of your data side by side.
= plt.subplots(1, 2, figsize=(14, 6)) # 1 row, 2 columns
fig, ax
# First subplot
0].bar(df['Compound'], df['Molecular Weight (g/mol)'], color='skyblue')
ax[0].set_title('Molecular Weights of Compounds')
ax[0].set_xlabel('Compound')
ax[0].set_ylabel('Molecular Weight (g/mol)')
ax[0].tick_params(axis='x', rotation=45)
ax[
# Second subplot
1].scatter(df['Molecular Weight (g/mol)'], df['Boiling Point (°C)'], color='salmon')
ax[1].set_title('Molecular Weight vs. Boiling Point')
ax[1].set_xlabel('Molecular Weight (g/mol)')
ax[1].set_ylabel('Boiling Point (°C)')
ax[
# Adjust layout to not overlap
plt.tight_layout() plt.show()
Note the tight_layout()
, which might be required to have nice looking non-overlapping plots.
Customizing Line Styles and Markers
You can customize the appearance of lines and markers to make your plots clearer and more visually distinct.
=(10, 6))
plt.figure(figsize'Compound'], df['Boiling Point (°C)'],
plt.plot(df[='darkred', linestyle='--', marker='^', markersize=10, linewidth=2,
color='Boiling Point')
label'Boiling Points of Compounds')
plt.title('Compound')
plt.xlabel('Boiling Point (°C)')
plt.ylabel(
plt.legend()=45)
plt.xticks(rotationTrue)
plt.grid( plt.show()
Adding Annotations
Annotations can be used to highlight specific points or features in your plots, such as identifying a compound with an unusually high or low boiling point.
=(10, 6))
plt.figure(figsize'Molecular Weight (g/mol)'], df['Boiling Point (°C)'], color='purple')
plt.scatter(df[
# Highlight the point for Acetic Acid
for i, row in df.iterrows():
= row['Boiling Point (°C)']
bp = row['Molecular Weight (g/mol)']
mw 'Compound'], (mw, bp),
plt.annotate(row[="offset points", xytext=(-10,10), ha='center', arrowprops=dict(arrowstyle='->', color='black'))
textcoords
'Molecular Weight vs. Boiling Point')
plt.title('Molecular Weight (g/mol)')
plt.xlabel('Boiling Point (°C)')
plt.ylabel(True) plt.grid(
Customizing Axes
Customizing the axes of your plot can improve readability and focus the viewer’s attention on the most relevant parts of your data.
=(10, 6))
plt.figure(figsize'Compound'], df['Molecular Weight (g/mol)'], color='teal')
plt.bar(df[
# Setting the range for the y-axis
0, 100)
plt.ylim(
# Customizing tick labels
=45, fontsize=12, color='blue')
plt.xticks(rotation=12, color='blue')
plt.yticks(fontsize
'Molecular Weights of Compounds', fontsize=16)
plt.title('Compound', fontsize=14) plt.xlabel(
Text(0.5, 0, 'Compound')
Using Colormaps
Colormaps can be used to add a color gradient to your plots, which is particularly useful for scatter plots to indicate density or another variable.
# Assuming an additional 'Density (g/mL)' column in the DataFrame
'Density (g/mL)'] = [1.0, 0.789, 1.049, 0.790, 0.791] # Example densities
df[
=(10, 6))
plt.figure(figsize= plt.scatter(df['Molecular Weight (g/mol)'], df['Boiling Point (°C)'],
sc =df['Density (g/mL)'], cmap='viridis')
c='Density (g/mL)')
plt.colorbar(sc, label
'Molecular Weight vs. Boiling Point by Density')
plt.title('Molecular Weight (g/mol)')
plt.xlabel('Boiling Point (°C)')
plt.ylabel(True) plt.grid(
Next, we will look at seaborn
, which is built on top of matplotlib
and makes it straightforward to beautify plots.