Today's blog post is dedicated to everyone whose eyes aren't as young as they used to be. In last week's blog post on pursuit curves, I showed this figure: If you ran my code exactly as I posted it, though, you probably wouldn't see exactly the same thing. This tutorial will discuss creating a horizontal line using the yline() function in Matlab. You would see something like this instead:ĭo you see the difference? Exactly what you see will vary depending on your computer and your display resolution, but the original image from my blog post has thicker colored lines. Plot a Horizontal Line Using the yline() Function in MATLAB. Here's a basic example of plot from the MATLAB documentation: x = linspace(-2*pi,2*pi) That's because I don't use the default line thickness for my plots. The plot above uses the default MATLAB line width of 0.5 points. Here's where I have to wave my hands a little. Because of the way the figure above was captured for display in your browser, the lines probably appear a little thicker than 0.5 points. On a high resolution display, however, the plotted lines are pretty close to 0.5 points thick.Īnd, to my eyes, that's too thin to see the colors clearly. So, I like to plot thicker lines, roughly 1.5-2.0 points. You can do that in each call to plot by using the LineWidth parameter, like this: plot(x,y1,x,y2, 'LineWidth',2.0)īut then you have to remember to add the LineWidth parameter all the time. It turns out that there's a way to get MATLAB to draw all plotted lines thicker by default. Here it is: set(groot, 'defaultLineLineWidth',2.0) The second (optional) return argument S is a MATLAB structure that. xline( x ) creates a vertical line at one or more x-coordinates in the current axes. xline) plot best fit line hold on line(xline, yline) The same commands can be used. For example, xline(2) creates a line at x2. This odd-looking line of code sets the default LineWidth property for line objects to 2.0. For a full explanation of the MATLAB system for setting default graphics object properties, see Default Property Values in the MATLAB documentation. Update: Using a thicker line width might only work well for high-DPI monitors. Blog reader Gang Yao pointed out that using 2-pt. lines can make markers indistinct, and I can confirm that observation when not using a high-DPI monitor. After some additional experimentation, I found that using 1.5-pt. lines on a high-DPI monitor works a little better. This default setting does not persist between MATLAB sessions, though. If you want to do this for each MATLAB session, put the call to set into a file called startup.m that is located in your user path folder. xline (x) creates a vertical line at one or more x -coordinates in the current axes. Run the userpath function to see where that is. For example, xline (2) creates a line at x2. xline (x,LineSpec) specifies the line style, the line color, or both. '/Users/steve/OneDrive - MathWorks/MATLAB' For example, xline ( 12 20 33,'-b') creates three dashed blue lines. (That's what I did.)įor more information, see the function reference pages for startup and userpath.You can easily adjust the thickness of lines in Matplotlib plots by using the linewidth argument function, which uses the following syntax: You can also use the userpath function to change your user path location, if you don't like the default location chosen by MATLAB. (x, y, linewidth=1.5)īy default, the line width is 1.5 but you can adjust this to any value greater than 0. Example 1: Adjust the Thickness of One Line This tutorial provides several examples of how to use this function in practice. #create line plot with line width set to 3Įxample 2: Adjust the Thickness of Multiple Lines Import numpy as np #define x and y values The following code shows how to create a simple line chart and set the line width to 3: import matplotlib. The MATLAB function xline() creates and draws vertical lines at a specified point on the x-axis of a graph. The following code shows how to adjust the thickness of multiple lines at once: import matplotlib. Hi there, Can anyone please tell me how I can add a vertical line to my plot at a specified sample point For example, I have a a 1x41 vector of intensity. pyplot as pltĮxample 3: Adjust Line Thickness in Legends It also provides the ability to place text labels and specify the format and attributes of the line color, width, linetype, etc. The following code shows how to create multiple lines with different thicknesses and create a legend that displays the the thickness of each line accordingly: import matplotlib.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |