Thursday, September 26, 2024

Plotting multiple lines using plt.plot()

So far we have created simple plot using matplot.pyplot.plot() funcion. In previous examples the line plot was used to create a line between the points and these points were define in form of x and y coordinates. For more information please check Getting started with basic Matplotlib plots.
In this tutorial we will create a line plot with two lines and these lines will connect the data points defined in the form of x and y coordinates stored in x and y1 lists for first line, and in x and y2 lists for the second line. It can be noticed that both lines used the same x coordinate values. \begin{eqnarray} x &=& [1,2,3,4,5,6]\\ \nonumber y_1 &=& [2,4,6,8,10,12]\\ \nonumber y_2 &=& [3,6,9,12,15,18] \end{eqnarray} The first step is to load required libraries. As in previous cases the only required library is the matplotlib and the module is pyplot.
import matploltib.pyplot as plt
The next step is to defined lists of x, y1, and y2 variables. These are coordinates for two line plots that will be plotted.
x = [1,2,3,4,5,6]
y1 = [2,4,6,8,10,12]
y2 = [3,6,9,12,15,18]
The next step is to define the figure size and as defined in previous examples we will use the figure size of 12 by 8 inches.
plt.figure(figsize=(12,8))
To plot the two lines on the same graph we will have to use plt.plot() function twice. The arguments inside the first plot function will be x and y1 lists and in second x and y2. Since there are two lines plots on the same graph it is a good practice to distinguish them so the third argument in each plot function will be label. The label in the first plot function will be $y = 2x$ and the label of the second plot function will be $y = 3x$.
plt.plot(x,y1, label="y = 2*x")
plt.plot(x,y2, label="y = 3*x")
These labels are needed to create a plot legend which shows how to distinguish each of the curves in the plot. The plot legend in Matplotlib is a box that labels the different elements or data series in a plot, such as lines, markers, or bars. It helps to distinguish between multiple data sets by showing which color or style corresponds to each label. This makes the plot easier to interpret and understand. \newline The remaining elements of the multiple line plot are to define the title, xlabel, ylabel, legend, grid and finally to show the plot. The title plt.title() is used to define the title of the plot, the plt.xlabel() and plt.ylabel() are used to define the labels of x and y axes. The plt.legend() is a new function (it was not used in previous examples) and to show the plot legend it is enough to define it as plt.legend(). The customization and position of the plot legened will be explained in future examples. It should be noted that if the labels are not defined inside the plot functions the legend function will have no effect on the matploltib plot which means that legend would not exist in the multi-line plot.\newline The remaining functions are grid and show i.e. the grid is used to show the grid inside the matplotlib plot and the show function is used to display the matplotlib multi-line plot.
plt.title("scatter plot example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.grid(True)
plt.show()
The entire code used in this example is shown below.
import matplotlib.pyplot as plt 
x = [1,2,3,4,5,6]
y1 = [2,4,6,8,10,12]
y2 = [3,6,9,12,15,18]
plt.figure(figsize=(12,8))
plt.plot(x,y1, label="y = 2*x")
plt.plot(x,y2, label="y = 3*x")
plt.title("scatter plot example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.grid(True)
plt.show()
When this code is compiled the result is shown in Figure Figure 1.
2024-09-25T22:07:47.739749 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 1 - Multiline plot

Wednesday, September 25, 2024

How to create scatter plot using matplotlib?

To plot your data in form of points on matplotlib graph the plt.scatter() function is used. The plt.scatter() function requires two arguments i.e. the x and y coordinates of the data points you wish to plot. The full from of the matplotlib.pyplot.scatter() function is
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
            
As stated the x and y are mandatory and they are the x and y coordinates of the data points you wish to plot using the matplotlib pyplot scatter function. The size of the x list/array must be equal to the list/array. The other parameters are set by default and you do not need to define them. \newline The s = None is he maker size in points**2 (typographic points are 1/72 in.). By setting to the None the default value is rcParams['lines.markersize']**2.\newline The c = None are the marker colors. IF set to None in that case te marker color is determined by the value of color, facecolor,or facecolors. The marker = None is the marker style. The marker can be either and instance of the class or the next shorthand for a particular marker. If set to None the default value is rcParams['scatter.marker'] or 'o'.
  • cmap=None - is the colormap instance or registered colormap name used to map scalar data to colors. This parameter is ignored if \(c\) is the RGBA.Since by default the cmap is set to None the colormap value is equal to rcParams['image.cmap'] or 'viridis'.
  • norm - this is short for normalization method which can be used to scale scalar data to the [0,1] range before mapping to colors using cmap.By default a linear scaling is used, mapping the lowest value to 0 and the highest to 1.
  • vmin, vmax = None - These set the minimum (vmin) and maximum (vmax) data values for the colormap scaling. By default these parameters are set to None so the minimum and maximum of the data are automatically taken. When using the colormap, vmin and vmax allow you to control the range of data values that the colormap covers.
  • alpha=None - this parameter controls the transparency level of points in the scatter plot. The value should be in 0 to 1 range where 0 is completely transparent and 1 is completely opaque. By default this parameter is equal to None which means that all points are fully opaque.
  • linewidths = None - this parameter controls the width of the edges around each scatter point. By default the value is None which means that default width is used. Larger values will make the edges thicker, while smaller values make them thinner.
  • edgecolors = None - is the parameter that specifies the edge color(s) of the edges around the scatter points. This can be a single coloror a sequence of colors. Common values include 'face' (same as the point's face color), 'none' (no edges), or any valid color format in Matplotlib.
  • plotnonfinite = False - the parameter determines whether to plot points that are not finite (like NaN or Inf). The default value is equal to False which means that non-finite points are not plotted. If the parameter is set to True, such points will appear in the plot.
  • data = None - the parameter allows passing a dictionary or a DataFrame that provides the data for the scatter plot. It can be used to reference column names directly, making it easier to manage data when working with Pandas or similar libraries.
  • **kwargs - This represents additional keyword arguments that can be passed to customize the scatter plot further, such as marker style, colors, labels, etc. Any valid Matplotlib property can be passed through **kwargs.

Example - First scatter plot

In this example we will crate the coordinates of data points in form of lists and use plt.scatter() function to plot this data points. The points are define in form of two lists x and y coordinates. \begin{eqnarray} x &=& [1,2,3,4,5]\\ \nonumber y &=& [2,4,6,8,10] \end{eqnarray} The first step was to define required libraries. Since we do not need numpy i.e. both variables are defined using built-in python lists the only library that must be defined is the matplotlib with pyplot module.
import matplotlib.pyplot as plt
Next we are going to define the x and y variable.
x = [1,2,3,4,5]
y = [2,4,6,8,10]
The we will define the the figure size to 12 by 8 inches, call the scatter function and define the x and y arguments, define the title (scatter plot example) name the x and y axes using xlabel and ylabel commands, define the grid, and finally show the graph.
plt.figure(figsize=(12,8))
plt.scatter(x,y)
plt.title("scatter plot example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()
The entire code and the result are given below.
import matplotlib.pyplot as plt 
x = [1,2,3,4,5]
y = [2,4,6,8,10]
plt.figure(figsize=(12,8))
plt.scatter(x,y)
plt.title("scatter plot example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()
After executing the entire code the result is shown in Figure 1.
2024-09-25T06:13:33.601600 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 1 - Scatter plot

Tuesday, September 24, 2024

Getting started with basic Matplotlib plots

In this post we will create first plot using the matplotlib library. This will be a simple line plot. Then we will cover the basics of the plot such as figure, axes and plot elements. After line plot we will create the scatter plot i.e. plot the data pots using the scatter plot. The next step would be to learn how to customize your plots and finally to learn how to save and export your plots.

Creating your first plot (plt.plot())

For first plot using matplotlib library we will create a line graph. The line graph is created using the plot function of the matplotlib library. The plt.plot() function in general form is written as:
matplotlib.pyplot.plot(*args, scalex = True, scaley = True, data = None, **kwargs)
            
The parameters inside the plot function are:
  • *args - this is a flexible parameter that can take different types of inputs. The examples of these inputs are:
    • a single sequence for \(y\) values (the x values will default to range(len(y))),
    • two sequences \(x\) and \(y\) data points,
    • multiple x and y pairs (useful for plotting multiple lines).
      plot([1,2,3,4])
      plot([1,2,3,4], [10,20,35,50])
      plot([1,2], [3,4], [5,6], [7,8])
  • scalex = True - this parameter automatically scales the x-axis to the data if set to True. If False, it will not rescale even if the data exceeds the current axis limits.
  • scaley = True - this parameter works the same as the scalex but for the y-axis. If True, if automatically scales the y-axis to the data.
  • data = None - this allows you to pass a data structure (dictionary or pandas DataFrame), so that instead of passing actual arrays or sequences for \(x\) and \(y\), you can refer to the names or keys of the data. This is useful for plotting irectlyfrom complex data structures i.e.
    data = {'x': [1,2,3,4], 'y':[10,20,25,30]}
    plot('x', 'y', data=data)
    
  • **kwargs - These are additional keyword arguments that can be used to customize the plot such as: color, linestyle, marker, label...
In this example we will plot the line plot that connects points with the following x and y coordinates. \begin{eqnarray} x &=& [1,2,3,4] \\ \nonumber y &=& [23,445,567,3] \end{eqnarray} The first step is to define the required libraries. If you have correctly installed the matplotlib library then type in the following code in your Python script.
import matplotlib.pyplot as plt
The second step is to define the data points (coordinates) that will be plotted using the line plot.
x = [1,2,3,4,5,6]
y = [2,4,7,8,10,12]
When the library and the data points are defined then we can define the required matplotlib functions to show the line graph. In this case we will need to define the figure size using plt.figure(figsize=(12,8)) function,plt.plot for creating the line plot, grid to display the grid to improve the readability, xlabel and ylabel to show the labels of the x and y axes, and finally the show function to display the line plot.
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
The entire code created in this example is shwon below.
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6]
y = [2,4,7,8,10,12]
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
When the previous code is executed the line plot is generated as shwon in Figure.
2024-09-25T02:05:20.622345 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 1 - Line plot created using pyplot.plot() funciton.
The previous block of code responsible for creating line plot in Figure 1 consist of the following matplotlib.pyplot functions:
  • plt.figure(figsize=(12,8))
    • plt.figure()- the plt is the common alias for the matplotlib pyplot module (it was previously explained when we imported the matplotlib library).
    • figure() - is the function that creates new figure in which you can plot your data If you forget to define it explicitly, the matplotlib will create one automatically when you start plotting. However the default figsize would be 6.4 by 4.8 which is width and height in inches.
    • figsize=(12,8) - - the figsize is the parrameter that specifies the sie fo the figure in inches. The parameter is a tuple that contains two values:
      • width - the first value in the tuple (12) represents the width of the figure.
      • height - the second vlaue (8) represents the height of the figure
    • numbers 12 and 8 - the size is given in inches beacuse Matplotlib uses inches as the unit of measure for figure dimensions. A figure with a size of (12,8) will be 12 inches wide and 8 inches tall.
    • dots per inch - the actual number of pixels in the figure is determined by the DPI setting and its 100 DPI by default. So , a figure with a 12x8 would have 1200 x 800 pixle size (12 inches * 100 DPI by 8 inches * 100 DPI).
  • plt.plot(x,y) - the plot() function is the pyplot function used for creating the 2D plots, usually a line plot, based on the provided data. The x and y are data points that you want to plot. The x represents the data for the x-axis.It is usally a list, array, or other iterable containing numerical values.y represents the data for the y-axis. As the x the y can also be a list, array, or another iterable containing numerical values. However, the length of the y must be the same as x.The plot function will connect each pair of points (x[i],y[i]) whit a line, creating a continuous line plot. The command matches each element in x with the corresponding element in y. For example x = [1,2,3,4,5,6] and y = [2,4,7,8,10,12] the function plots the points (1,2), (2,4), (3,7), (4,8), (5,10), and (6,12) and connects them with a line.
  • plt.grid(True) - This function is used to toggle the visibility of the grid lines on the plot. The True argument turns on the grid lines. This means the grid lines will be displayed on the plot. In case the False value is inside the brackets it will turn off the grid lines. The puprose of grid lines is to enhance the readability i.e. grid lines help improve the readability of the plot by making it easire to aling and iterpret data points, especially when the plot contains multple lines or data points.
  • plt.xlabel('x'), plt.ylabel('y')- xlabel and ylabel are functions used to set a label for x-axis and y axis of the current plot. The x or y argument are labels you want to display along the x and y-axis respectively. For example try changing the xlabel argument to "time (s)" and the ylabel argument to "Temperature [K]" and see what happens.
  • plt.show() - is the matplotlib command that displays the current plot or figure. It opens a window with the plot, allowing you to visualize the data. This function is essential for creating plots in scripts or interactive enviroments, as it renders the plot on the screen. After the commnad is called, the figure is displayed and the script execution continues or it ends if this sis the last command in the script.