How do i make a line in python?
I would like to know how to draw a line using the x and y coordinates of two 2-dimensional points. I tried the turtle graphics, but it works using degrees. Show
nbro 14.3k27 gold badges104 silver badges188 bronze badges asked Oct 20, 2015 at 15:14
1 Depending of your needs for plotting you can use matplotlib
answered Oct 20, 2015 at 15:23
efirvidaefirvida 4,3592 gold badges39 silver badges62 bronze badges
Your premise doesn't hold -- turtle can do it, no degrees needed:
answered May 2, 2018 at 22:13
cdlanecdlane 38.2k5 gold badges27 silver badges74 bronze badges You could make use of pygame depending on what you are doing it for as it allows a similar:
For Example, when the environment has been set up:
can draw: answered Oct 20, 2015 at 15:23
1 If you are
already using
answered Oct 20, 2015 at 15:28
volentvolent 4736 silver badges16 bronze badges 2 You could calculate the angle from the 4 points using the following formula
Just a warning, depending on the math library you use, this will probably output in radians. However you can convert radians to degrees using the following formula.
answered Oct 20, 2015 at 15:19
Rob S.Rob S. 9236 silver badges21 bronze badges Just for completeness, you can also use the ImageDraw module of Pillow (Python Image Library / PIL fork). That way you don't need a window and you can save the drawn image into a file instead.
answered Aug 2, 2018 at 23:33
Perhaps the simplest of all plots is the visualization of a single function $y = f(x)$. Here we will take a first look at creating a simple plot of this type. As with all the following sections, we'll start by setting up the notebook for plotting and importing the packages we will use: In [1]: For all Matplotlib plots, we start by creating a figure and an axes. In their simplest form, a figure and axes can be created as follows: In [2]: fig = plt.figure() ax = plt.axes() In Matplotlib, the figure (an instance of the class Once we have created an axes, we can use the In [3]: fig = plt.figure() ax = plt.axes() x = np.linspace(0, 10, 1000) ax.plot(x, np.sin(x)); Alternatively, we can use the pylab interface and let the figure and axes be created for us in the background (see Two Interfaces for the Price of One for a discussion of these two interfaces): If we want to create a single figure with multiple lines, we can simply call the In [5]: plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)); That's all there is to plotting simple functions in Matplotlib! We'll now dive into some more details about how to control the appearance of the axes and lines. Adjusting the Plot: Line Colors and Styles¶The first adjustment you might wish to make to a plot is to control the line colors and styles. The
In [6]: plt.plot(x, np.sin(x - 0), color='blue') # specify color by name plt.plot(x, np.sin(x - 1), color='g') # short color code (rgbcmyk) plt.plot(x, np.sin(x - 2), color='0.75') # Grayscale between 0 and 1 plt.plot(x, np.sin(x - 3), color='#FFDD44') # Hex code (RRGGBB from 00 to FF) plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 to 1 plt.plot(x, np.sin(x - 5), color='chartreuse'); # all HTML color names supported If no color is specified, Matplotlib will automatically cycle through a set of default colors for multiple lines. Similarly, the line style can be
adjusted using the In [7]: plt.plot(x, x + 0, linestyle='solid') plt.plot(x, x + 1, linestyle='dashed') plt.plot(x, x + 2, linestyle='dashdot') plt.plot(x, x + 3, linestyle='dotted'); # For short, you can use the following codes: plt.plot(x, x + 4, linestyle='-') # solid plt.plot(x, x + 5, linestyle='--') # dashed plt.plot(x, x + 6, linestyle='-.') # dashdot plt.plot(x, x + 7, linestyle=':'); # dotted If you would like to be extremely terse, these In [8]: plt.plot(x, x + 0, '-g') # solid green plt.plot(x, x + 1, '--c') # dashed cyan plt.plot(x, x + 2, '-.k') # dashdot black plt.plot(x, x + 3, ':r'); # dotted red These single-character color codes reflect the standard abbreviations in the RGB (Red/Green/Blue) and CMYK (Cyan/Magenta/Yellow/blacK) color systems, commonly used for digital color graphics. There are many other keyword arguments that can be used to fine-tune the appearance of the plot; for more details, I'd suggest viewing the docstring of the Adjusting the Plot: Axes Limits¶Matplotlib does a decent job of choosing default axes limits for your plot, but sometimes it's nice to have finer control. The most basic way to adjust axis limits is to use the In [9]: plt.plot(x, np.sin(x)) plt.xlim(-1, 11) plt.ylim(-1.5, 1.5); If for some reason you'd like either axis to be displayed in reverse, you can simply reverse the order of the arguments: In [10]: plt.plot(x, np.sin(x)) plt.xlim(10, 0) plt.ylim(1.2, -1.2); A useful related method is In [11]: plt.plot(x, np.sin(x)) plt.axis([-1, 11, -1.5, 1.5]); The In [12]: plt.plot(x, np.sin(x)) plt.axis('tight'); It allows even higher-level specifications, such as ensuring an equal aspect ratio so that on your screen, one unit in In [13]: plt.plot(x, np.sin(x)) plt.axis('equal'); For more information on axis limits and the other capabilities of the Labeling Plots¶As the last piece of this section, we'll briefly look at the labeling of plots: titles, axis labels, and simple legends. Titles and axis labels are the simplest such labels—there are methods that can be used to quickly set them: In [14]: plt.plot(x, np.sin(x)) plt.title("A Sine Curve") plt.xlabel("x") plt.ylabel("sin(x)"); The position, size, and style of these labels can be adjusted using optional arguments to the function. For more information, see the Matplotlib documentation and the docstrings of each of these functions. When multiple lines are being shown within a single axes, it can be useful to create a plot legend that labels each line type. Again, Matplotlib has a built-in way of quickly creating such a legend. It is done via the (you guessed it) In [15]: plt.plot(x, np.sin(x), '-g', label='sin(x)') plt.plot(x, np.cos(x), ':b', label='cos(x)') plt.axis('equal') plt.legend(); As you can see, the Aside: Matplotlib Gotchas¶While most
In the object-oriented interface to plotting, rather than calling these functions individually, it is often more convenient to use
the In [16]: ax = plt.axes() ax.plot(x, np.sin(x)) ax.set(xlim=(0, 10), ylim=(-2, 2), xlabel='x', ylabel='sin(x)', title='A Simple Plot'); How do you create a line in Python?line() Draws a line between the coordinates in the xy list. Parameters: xy – Sequence of either 2-tuples like [(x, y), (x, y), …] or numeric values like [x, y, x, y, …]. fill – Color to use for the line.
How do you draw a straight line in Python?The axhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis. Parameters: y: Position on Y axis to plot the line, It accepts integers.
How do you print a blank line in Python?Use print() without any arguments to print a blank line
Call print() without any arguments to output a blank line.
How do you draw a line between two points in Python?Use matplotlib.. point1 = [1, 2]. point2 = [3, 4]. x_values = [point1[0], point2[0]] gather x-values.. y_values = [point1[1], point2[1]] gather y-values.. plt. plot(x_values, y_values). |