Module 1 Matplotlib : Introduction Line Plots in Matplotlib

Visualize our data’s temporal patterns and trends

Agusmahari
Data Engineering Indonesia
22 min readApr 22, 2024

--

Photo by Isaac Smith on Unsplash

Matplotlib is a widely used and powerful data visualization library in Python. It provides various functions that allow users to create high-quality graphs, ranging from histograms to scatter plots. One of the most commonly used plot types is the line plot. Line plots are particularly useful for displaying trends in data over time or in a specific sequence. This article will discuss how to create line plots using Matplotlib, including some features that can enhance the visual appeal of your graphs.

Understanding Line Plots

A line plot, also known as a line graph, is a type of chart that connects a series of data points usually in a meaningful sequence (such as time series) with line segments. It is an effective tool for showing changes in data over time.

Why Use Line Plots?

  • Trends: Line plots help in identifying upward or downward trends in data.
  • Comparison: They facilitate comparison between two sets of data over time.
  • Continuity: Visualizes the continuity of data, showing how one data point transitions to the next.

Create a Line Plot

Using this data, we can create a simple line plot:

import pandas as pd
import matplotlib.pyplot as plt

# Data
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200]
}

df = pd.DataFrame(data)
df['Tanggal'] = pd.to_datetime(df['Tanggal']) # convert columm 'Tanggal' to format datetime

1. plt.plot()

This is the core function in Matplotlib for creating line plots. You can provide a series of x and y values as parameters, as well as various options to customize the appearance of the line:

  • x, y: These are arrays or lists of values to be plotted.
  • color: Sets the color of the line. Can be in the form of color names (‘red’, ‘blue’) or hex codes (‘#008000’).
  • linestyle: Sets the style of the line, such as solid (‘-’), dashed (‘ — ‘), dotted (‘:’), dashdot (‘-.’).
  • marker: Adds markers for each data point, like circles (‘o’), stars (‘*’), or squares (‘s’).

Example usage:

import pandas as pd
import matplotlib.pyplot as plt

# Data yang diberikan
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200]
}

df = pd.DataFrame(data)
df['Tanggal'] = pd.to_datetime(df['Tanggal']) # convert columm 'Tanggal' to format datetime
plt.plot

NOTE
plt.tight_layout()
:
plt.tight_layout(): This function is used to optimize the layout of the elements within the figure so that they look tidy and do not overlap. It ensures that all plot elements, such as axis labels and title, are properly placed within the figure.

plt.show():
plt.show(): This function is used to display the created plot. Without this, the plot would only be stored in memory and not shown to the user.

import pandas as pd
import matplotlib.pyplot as plt

# Data
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200]
}

df = pd.DataFrame(data)
df['Tanggal'] = pd.to_datetime(df['Tanggal']) # convert columm 'Tanggal' to format datetime

plt.plot(df['Tanggal'], df['Harga Saham'])
plt.tight_layout()
plt.show()
import pandas as pd
import matplotlib.pyplot as plt

# Data
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200]
}

df = pd.DataFrame(data)
df['Tanggal'] = pd.to_datetime(df['Tanggal']) # convert columm 'Tanggal' to format datetime

plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='HargaSaham', # Sets the label for the line in the legend to 'HargaSaham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

plt.tight_layout()
plt.show()

2. plt.figure()

This function is used to create a new figure, which you can use to control various visualization aspects like figure size and resolution.

  • plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100):
  • plt.figure(): This function is used to create a new figure or canvas for the plot.
  • figsize=(16, 6): Specifies the size of the figure to be created. It sets the width to 16 inches and the height to 6 inches.
  • facecolor='lightgrey': Sets the background color of the figure to 'lightgrey'. This will change the background color of the entire figure canvas.
  • dpi=100: Sets the resolution or dots per inch of the figure to 100. A higher DPI value results in a higher resolution image when saving the plot, but it also increases the file size.

Example usage:

# Import required libraries
import pandas as pd # For data manipulation and analysis
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Adjust layout to prevent clipping of labels and display the plot
plt.tight_layout()

# Display the plot
plt.show()
plt.figure

3. plt.xlabel() and plt.ylabel()

These functions add labels to the x and y axes, which are crucial for clarity in interpreting the displayed data.

Example usage:

# Import required libraries
import pandas as pd # For data manipulation and analysis
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Set x-axis label with specific font size
plt.xlabel('Tanggal', fontsize=12)

# Set y-axis label with specific font size
plt.ylabel('Harga Saham', fontsize=12)

# Display the plot
plt.show()

4. plt.title()

Adds a title to the plot. The title provides a brief overview of what the data represented in the graph is about.

Example usage:

plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green',
loc='center',
color='white')
# Import required libraries
import pandas as pd # For data manipulation and analysis
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Set x-axis label with specific font size
plt.xlabel('Tanggal', fontsize=12)

# Set y-axis label with specific font size
plt.ylabel('Harga Saham', fontsize=12)

# Set plot title with specific font size, background color, and alignment
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Sets the background color of the title to green
loc='center', # Sets the alignment of the title to center
color='white') # Sets the text color of the title to white

# Adjust layout to prevent clipping of labels and display the plot
plt.tight_layout()

# Display the plot
plt.show()
plt.title

5. plt.legend()

Displays a legend explaining each dataset on the plot. It’s very useful when multiple lines are plotted on a single figure.

Example usage:

plt.legend(fontsize=10, loc="lower left")
# Import required libraries
import pandas as pd # For data manipulation and analysis
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Set x-axis label with specific font size
plt.xlabel('Tanggal', fontsize=12)

# Set y-axis label with specific font size
plt.ylabel('Harga Saham', fontsize=12)

# Set plot title with specific font size, background color, and alignment
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Sets the background color of the title to green
loc='center', # Sets the alignment of the title to center
color='white') # Sets the text color of the title to white

# Add legend to the plot with specific font size and location
plt.legend(fontsize=10, loc="lower left")

# Adjust layout to prevent clipping of labels and display the plot
plt.tight_layout()

# Display the plot
plt.show()

6. plt.xlim() and plt.ylim()

Sets the limits for the x and y axes, allowing you to focus or zoom into a specific area of the data.

Example usage:

plt.xlim('2022-01-01', '2022-01-11')  # Batas sumbu x
plt.ylim(55, 58) # Batas sumbu y
plt.xlim(df['Tanggal'].min(), df['Tanggal'].max())  # Batas sumbu x
plt.ylim(df['Harga Saham'].min() - 5, df['Harga Saham'].max() + 5) # Batas sumbu y dengan tambahan 5 unit untuk memperlihatkan ruang yang cukup
import pandas as pd
import matplotlib.pyplot as plt

# Data yang diberikan
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200]
}

df = pd.DataFrame(data)
df['Tanggal'] = pd.to_datetime(df['Tanggal']) # Mengonversi kolom 'Tanggal' ke format datetime

plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='HargaSaham', # Sets the label for the line in the legend to 'HargaSaham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

plt.xlabel('Tanggal', fontsize=12) # font label x
plt.ylabel('Harga Saham', fontsize=12) # font label y
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green',
loc='center',
color='white')
plt.legend(fontsize=10, loc="lower left")
# plt.xlim('2022-01-03', '2022-01-08') # Batas sumbu x
# Mengatur batas sumbu x dengan format datetime
plt.xlim(pd.Timestamp('2022-01-03'), pd.Timestamp('2022-01-08'))
plt.ylim(35000, 36750) # Batas sumbu y
plt.tight_layout()
plt.show()

7. plt.xticks() and plt.yticks()

These functions set the locations and labels of the tick marks on the x and y axes, allowing for further customization of how the data is displayed.

Example usage:

plt.xticks(df['Tanggal'], rotation=45)  # Label sumbu x berdasarkan data tanggal dengan rotasi 45 derajat
plt.yticks(np.arange(55, 59, 1)) # Label sumbu y dari 55 hingga 58 dengan interval 1
# Import necessary libraries
import pandas as pd # For data manipulation and analysis
import numpy as np # For numerical operations
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Set x-axis label with specific font size
plt.xlabel('Tanggal', fontsize=12)

# Set y-axis label with specific font size
plt.ylabel('Harga Saham', fontsize=12)

# Set plot title with specific font size, background color, and alignment
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Sets the background color of the title to green
loc='center', # Sets the alignment of the title to center
color='white') # Sets the text color of the title to white

# Add legend to the plot with specific font size and location
plt.legend(fontsize=10, loc="lower left")

# Set x-axis ticks with specific rotation
plt.xticks(df['Tanggal'], rotation=45)

# Set y-axis ticks with a specific range
plt.yticks(np.arange(35000, 36750, 200))

# Adjust layout to prevent clipping of labels and display the plot
plt.tight_layout()
plt.show()

8. plt.grid()

Adds grid lines to the plot, making the data easier to read and understand.

Example usage:

plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')  # Garis grid dengan style dashed dan warna abu-abu
# Import necessary libraries
import pandas as pd # For data manipulation and analysis
import numpy as np # For numerical operations
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Set x-axis label with specific font size
plt.xlabel('Tanggal', fontsize=12)

# Set y-axis label with specific font size
plt.ylabel('Harga Saham', fontsize=12)

# Set plot title with specific font size, background color, and alignment
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Sets the background color of the title to green
loc='center', # Sets the alignment of the title to center
color='white') # Sets the text color of the title to white

# Add legend to the plot with specific font size and location
plt.legend(fontsize=10, loc="lower left")

# Set x-axis ticks with specific rotation
plt.xticks(df['Tanggal'], rotation=45)

# Set y-axis ticks with a specific range
plt.yticks(np.arange(35000, 36750, 200))

# Add grid to the plot with specific linestyle, linewidth, and color
plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')

# Adjust layout to prevent clipping of labels and display the plot
plt.tight_layout()
plt.show()

9. plt.savefig()

Saves the plot to a file. You can specify the file format (e.g., PNG, PDF, SVG) and other parameters like dpi.

Example usage:

# Import necessary libraries
import pandas as pd # For data manipulation and analysis
import numpy as np # For numerical operations
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure with specific size, background color, and dpi
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='Harga Saham', # Sets the label for the line in the legend to 'Harga Saham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

# Set x-axis label with specific font size
plt.xlabel('Tanggal', fontsize=12)

# Set y-axis label with specific font size
plt.ylabel('Harga Saham', fontsize=12)

# Set plot title with specific font size, background color, and alignment
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Sets the background color of the title to green
loc='center', # Sets the alignment of the title to center
color='white') # Sets the text color of the title to white

# Add legend to the plot with specific font size and location
plt.legend(fontsize=10, loc="lower left")

# Set x-axis ticks with specific rotation
plt.xticks(df['Tanggal'], rotation=45)

# Set y-axis ticks with a specific range
plt.yticks(np.arange(35000, 36750, 200))

# Add grid to the plot with specific linestyle, linewidth, and color
plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')

# Save the plot as an image with specific dpi and bounding box settings
plt.savefig('harga_saham_2022.png', dpi=300, bbox_inches='tight')

# Adjust layout to prevent clipping of labels and display the plot
plt.tight_layout()
plt.show()

10. plt.show()

Displays the created plot. In a Jupyter notebook, the plot is usually displayed automatically, but in a Python script or other environments, you need to call this function.

Example usage:

# Import required libraries
import pandas as pd # For data manipulation and analysis
import numpy as np # For numerical operations
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Sets the color of the line to blue
label='HargaSaham', # Sets the label for the line in the legend to 'HargaSaham'
linestyle='--', # Sets the line style to dashed (--)
linewidth=2, # Sets the line width to 2 points
marker='o', # Sets the marker for data points to a circle
markersize=6, # Sets the size of the marker to 6 points
markerfacecolor='red', # Sets the fill color of the marker to red
markeredgewidth=2, # Sets the edge width of the marker to 2 points
markeredgecolor='black' # Sets the edge color of the marker to black
)

plt.xlabel('Tanggal', fontsize=12) # font label x
plt.ylabel('Harga Saham', fontsize=12) # font label y
# Set plot title
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Background color of title
loc='center', # Title alignment
color='white') # Title text color

# Add legend to the plot
plt.legend(fontsize=15, loc="upper left")

# Set x and y axis ticks
plt.xticks(df['Tanggal'], rotation=45) # Set x-axis ticks with rotation
plt.yticks(np.arange(35000, 36750, 200)) # Set y-axis ticks with specific range

# Add grid to the plot
plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')

# Save the plot as an image
plt.savefig('harga_saham_2022.png', dpi=300, bbox_inches='tight')

11. plt.annotate

plt.annotate(str(price), …: The annotate function from matplotlib.pyplot is used to add a label or annotation to the plot.

# Import required libraries
import pandas as pd # For data manipulation and analysis
import numpy as np # For numerical operations
import matplotlib.pyplot as plt # For plotting

# Define the data as a dictionary
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [
35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200]
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Convert 'Tanggal' column to datetime format
df['Tanggal'] = pd.to_datetime(df['Tanggal'])

# Set up the plot figure
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot the data
plt.plot(df['Tanggal'], df['Harga Saham'],
color='blue', # Set line color
label='Harga Saham', # Set legend label
linestyle='--', # Set line style
linewidth=2, # Set line width
marker='o', # Set marker shape
markersize=6, # Set marker size
markerfacecolor='red', # Set marker face color
markeredgewidth=2, # Set marker edge width
markeredgecolor='black' # Set marker edge color
)

# Annotate data points with their values
for i, harga in enumerate(df['Harga Saham']):
plt.annotate(str(harga), # Text to display
(df['Tanggal'][i], harga), # Position of the text
textcoords="offset points", # Text position relative to data point
xytext=(0,10), # Offset of text from data point
ha='center', # Horizontal alignment
color='blue', # Text color
fontsize=8) # Font size

# Set x and y axis labels
plt.xlabel('Tanggal', fontsize=12)
plt.ylabel('Harga Saham', fontsize=12)

# Set plot title
plt.title('Perubahan Harga Saham Selama Tahun 2022',
fontsize='20',
backgroundcolor='green', # Background color of title
loc='center', # Title alignment
color='white') # Title text color

# Add legend to the plot
plt.legend(fontsize=15, loc="upper left")

# Set x and y axis ticks
plt.xticks(df['Tanggal'], rotation=45) # Set x-axis ticks with rotation
plt.yticks(np.arange(35000, 36750, 200)) # Set y-axis ticks with specific range

# Add grid to the plot
plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')

# Save the plot as an image
plt.savefig('harga_saham_2022.png', dpi=300, bbox_inches='tight')

# Adjust layout and display the plot
plt.tight_layout()
plt.show()

By combining these functions, you can create complex and informative data visualizations in Python using Matplotlib.

Adding Multiple Lines di Matplotlib

Sometimes, we might want to compare more than one set of data in a single graph. With a line plot, you can easily add more than one line. For example, besides stock prices, let’s add synthetic data about bond prices and see how both prices change over time.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Data yang diberikan
data = {
'Tanggal': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Harga Saham': [35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200],
'Prediksi Harga Saham' : [35000, 35800, 35700, 36900, 36110, 35900, 36150, 36950, 36100, 36250, 36100]
}

df = pd.DataFrame(data)
df['Tanggal'] = pd.to_datetime(df['Tanggal'])
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Given data
data = {
'Date': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Stock Price': [35000, 35500, 36000, 36500, 36250,35800, 36250, 36750, 36300, 36050, 36200],
'Stock Price Prediction': [35000, 35800, 35700, 36900, 36110, 35900, 36150, 36950, 36100, 36250, 36100]
}

df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date']) # Convert 'Date' column to datetime format

plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)

# Plot Stock Price
plt.plot(df['Date'], df['Stock Price'],
color='blue',
label='Stock Price (Actual)',
linestyle='--',
linewidth=2,
marker='o',
markersize=6,
markerfacecolor='red',
markeredgewidth=2,
markeredgecolor='black'
)

# Plot Stock Price Prediction
plt.plot(df['Date'], df['Stock Price Prediction'],
color='green',
label='Stock Price Prediction',
linestyle='-',
linewidth=2,
marker='s',
markersize=6,
markerfacecolor='yellow',
markeredgewidth=2,
markeredgecolor='black'
)

# Add stock price value at each data point
for i, price in enumerate(df['Stock Price']):
plt.annotate(str(price),
(df['Date'][i], price),
textcoords="offset points",
xytext=(0,10),
ha='center',
color='blue',
fontsize=8)

# Add stock price prediction value at each data point
for i, prediction in enumerate(df['Stock Price Prediction']):
plt.annotate(str(prediction),
(df['Date'][i], prediction),
textcoords="offset points",
xytext=(0,10),
ha='center',
color='green',
fontsize=8)

plt.xlabel('Date', fontsize=12)
plt.ylabel('Stock Price', fontsize=12)
plt.title('Stock Price Changes During 2022',
fontsize='20',
backgroundcolor='green',
loc='center',
color='white')
plt.legend(fontsize=10, loc="lower left")
plt.xticks(df['Date'], rotation=45)
plt.yticks(np.arange(34500, 37250, 500))
plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')
plt.savefig('stock_price_2022_prediction.png', dpi=300, bbox_inches='tight')
plt.tight_layout()
plt.show()

Sometimes, it might be useful to display multiple graphs together to compare data. With Matplotlib, you can easily create subplots to show more than one graph within a single frame. This feature is particularly helpful when analyzing relationships or differences between two or more datasets in a visually cohesive manner. For example, let’s compare stock prices with bond prices in two separate plots but within a single frame. This setup allows viewers to directly compare the trends and fluctuations of both markets at the same time, providing a clearer understanding of how each market may influence or react to the same economic factors. By using subplots, you can maintain a structured layout that enhances both the aesthetic and functional clarity of the graphical representation.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Given data
data = {
'Date': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05','2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10', '2022-01-11'],
'Stock Price': [35000, 35500, 36000, 36500, 36250, 35800, 36250, 36750, 36300, 36050, 36200],
'Stock Price Prediction': [35000, 35800, 35700, 36900, 36110, 35900, 36150, 36950, 36100, 36250, 36100]
}

df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date']) # Convert 'Date' column to datetime format

# Plot for Actual Stock Price
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)
plt.plot(df['Date'], df['Stock Price'],
color='blue',
label='Stock Price (Actual)',
linestyle='--',
linewidth=2,
marker='o',
markersize=6,
markerfacecolor='red',
markeredgewidth=2,
markeredgecolor='black'
)

# Add stock price value at each data point
for i, price in enumerate(df['Stock Price']):
plt.annotate(str(price),
(df['Date'][i], price),
textcoords="offset points",
xytext=(0,10),
ha='center',
color='blue',
fontsize=8)

plt.xlabel('Date', fontsize=12)
plt.ylabel('Stock Price', fontsize=12)
plt.title('Actual Stock Price Changes During 2022',
fontsize='20',
backgroundcolor='blue',
loc='center',
color='white')
plt.legend(fontsize=10, loc="lower left")
plt.xticks(df['Date'], rotation=45)
plt.yticks(np.arange(34500, 37250, 250))
plt.grid(True, linestyle='--', linewidth=0.5, color='gray')
plt.tight_layout()
plt.show()

# Plot for Stock Price Prediction
plt.figure(figsize=(16, 6), facecolor='lightgrey', dpi=100)
plt.plot(df['Date'], df['Stock Price Prediction'],
color='green',
label='Stock Price Prediction',
linestyle='-',
linewidth=2,
marker='s',
markersize=6,
markerfacecolor='yellow',
markeredgewidth=2,
markeredgecolor='black'
)

# Add stock price prediction value at each data point
for i, prediction in enumerate(df['Stock Price Prediction']):
plt.annotate(str(prediction),
(df['Date'][i], prediction),
textcoords="offset points",
xytext=(0,10),
ha='center',
color='green',
fontsize=8)

plt.xlabel('Date', fontsize=12)
plt.ylabel('Stock Price Prediction', fontsize=12)
plt.title('Stock Price Predictions During 2022',
fontsize='20',
backgroundcolor='green',
loc='center',
color='white')
plt.legend(fontsize=10, loc="lower left")
plt.xticks(df['Date'], rotation=45)
plt.yticks(np.arange(34500, 37250, 250))
plt.grid(True, linestyle='-', linewidth=0.5, color='gray')
plt.tight_layout()
plt.show()

Having 3,5 years experience as IT Developer and Data Engineer. Enthusiats in Data Ingestion, Data Transformation & Warehouse, Data Concept, Analyze using google Cloud Platform, HDFS System. and Conten Writer In Medium [Link] Currently working with several data-related technologies such as IBM Data Stage, Hive, Python, Airflow, BigQuery, Redshift, Looker Studio, Kubernetes etc. Passionate about “Cooking Raw Data” into data that is ready to be processed by Data Analysts to produce useful information to assist in decision support. Learning to write on Medium as a platform for learning and sharing what I have learned.

You can connect with me on LinkedIn, Agusmahari@gmail.com

--

--

Agusmahari
Data Engineering Indonesia

Data Enginner | Big Data Platform at PT Astra International Tbk. Let's connect on Linkedin https://www.linkedin.com/in/agus-mahari/