Showing posts with label Python Tutorials. Show all posts
Showing posts with label Python Tutorials. Show all posts

Thursday, December 19, 2024

How to change the line style in Matplotlib?

The matplotlib offers a variety of line styles which could be used to customize the appearance of lines in your plot. The line styles include solid, dotted, dashed, dasheddot, loosley dotted, dotted, densely dotted, long dash with offset, loosely dashed, dashed, densely dashed, loosely dashdotted, dashdotted, densely dashdotted, dashdotdotted, loosely dashdotted, and densely dashdotted. In the following example we will create a simple plot. For this plot we need to import required librarires (numpy, matplotlib), create data that will be plotted, and plot the data using the plt.plot function. First step - import required libraires.
import numpy as np
import matplotlib.pyplot as plt
Second ste- generate the data that will be plotted. We will create x and y coordinates. The x corrdinates will be created using np.linspace function in 0 to 10 range and it will contain 100 values. The y values will be created using np.sin() function.
x = np.linspace(0,10,100)
y = np.sin(x)
Third step - Defining the plot parameters and showing the plot. The size of the plot will be defined using plt.figure and the size will be defined using the figsize parameter. We will set the size 12 by 8 inches. Then the line plot will be created using plt.plot function where arguments of this function are previously created x and y coordaintes. We will add the plt.title() function, set the plt.grid(True) function and finally show the plot using plt.show() function.
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.title("Simple Sine Function")
plt.grid(True)>
plt.show()
The result is shwon in Figure 1.
2024-12-20T00:11:15.554946 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 1 - simple plot of the size function.
In this example we will customize the line showing sine function by setting the line stlye to dashed which is done with linestyle parameter. Then we will set the linewidth or width of the line to 10 points, where 1 point equals 1/72 of an inch.Finally we will set the color of the line plot to black. The modificiations are shown in the following block.
plt.figure(figsize=(12,8))
plt.plot(x,y,linestyle='--', linewidth=10, color = 'black')
plt.title("Simple Sine Function")
plt.grid(True)
plt.show()
The results of the preivous code is shown in Figure 2.
2024-12-20T00:26:05.448472 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 2 - Sine plot with customized line
This tutorial demonstrated how to customize lines in Matplotlib to enhance your plots. If you have any comments, suggestions, or questions, feel free to share them below. Your feedback is always appreciated!

How to create a customized colormaps in Matplotlib?

Besides specifying named colors, hexadecimal colors, RGB and RGBA, and colormaps, the matplotlib offers the ability of defining your own colormap.This can be done us by combining the existing ones or by specifying the list of colors.
To create the custom colormap with specific colors at certain points along the color scale, and creating the smooth gradient between those certain points the LinearSegmentedColormap from matplotlib color module is used. When you specify the color list and apply the Linear Segemented Colors class will interpolate the colors in between, giving you the precise control over the appearance of the colormap. It is a very useful tool for creating unique color schemes tailored to your data visualizaton needs. To import the Linear SegementedColormap write the following code.
from matplotlib.colors import LinearSegmentedColormap
To show the example of how LinearSegmentColormap works we will need to plot some data. For data generation we will need the numpy library and for plotting this data we will use the maptlotlib.pyplot function.
import numpy as  np
import matplotlib.pyplot asplt
The data will be numpy matrix 10x10 with random vlaues from 0 to 1. To test this LinearSegmentedColormap class we will create a simple list containin three colors i.e. blue, green, and red.
colors = ['blue'. 'green', 'red']
The next step is to create a custom colormap and to do that LinearSegmentedColormap.from_list is needed The arguments in this function will be the name of the color list (string) which will be called ”my cmap”, and we also have to provide the list of colors. The newly created colormap will be stored under the custom_cmap variable.
custom_cmap = LinearSegmentedColormap.from_list("my_cmap", colors)
Now we will show the data in form of the heatmap using the plt.imshow() funciton.
plt.imshow(data, cmap = custom_cmap)
The entire code is shwon below and the imshow plot is shown in Figure 1.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
colors = ['blue', 'green', 'red']
custom_cmap = LinearSegmentedColormap.from_list("my_cmap", colors)
data = np.random.rand(10,10)
plt.imshow(data,cmap =custom_cmap)
2024-12-19T23:25:00.103456 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 1 - The ishow plot with custom colormap