How to Draw a 2d Plane in Python
Matplotlib is a Python-based plotting library similar to Matlab. It provides a similar fix of control APIs, which is very suitable for interactive drawing. We can easily use it as a drawing control and embed it in a GUI application. It contains a big number of tools that tin be used to create a variety of graphics, including simple linear graphs, scatter plots, sinusoids, and even iii-dimensional graphs.
Numpy is a Python library for mathematical operations. Information technology was installed when matplotlib was installed. Pyplot is a submodule of matplotlib, which mainly provides a land car interface for the underlying object-oriented drawing library. The state auto implicitly automatically creates numbers and coordinate axes to achieve the required drawing. Everything in matplotlib is organized according to hierarchical results. The top layer is the matplotlib "state auto surround" provided by pyplot. Based on this country machine environs, we can create graphics.
This article introduces the utilise of matplotlib to draw different two-dimensional graphics.
Basic drawing procedure:
-Create the canvas
-Add title, add 10 axis and Y axis name, modify the calibration and range of X axis and Y axis
-Describe graphics and adapt the graphic mode
-Add legend
-Display picture after cartoon
-Salve Picture
When using any tertiary-party library in Python, y'all must first import.
1 2
import matplotlib.pyplot as plt import numpy as np
The basic usage of matplotlib will not exist introduced in detail. The following introduces several two-dimensional graphs often drawn with matplotlib.
Line graph
Depict multiple graphics in i cartoon.
The code is:
1 2 3 four 5 vi vii 8 nine 10 xi 12 13
import matplotlib.pyplot every bit plt import numpy as np fig = plt.effigy() 10 = np.linspace(0, 2 * np.pi, fifty) y_sin = np.sin(ten) y_cos = np.cos(x) plt.championship('sin(x) & cos(ten)') plt.xlabel('10 centrality') plt.ylabel('Y centrality') plt.plot(ten, y_sin, color = "reddish", linewidth = ane.5, linestyle = "-.", label = "y_sin") plt.plot(x, y_cos, marking = '+', linestyle = '-', label = 'y_cos') plt.legend(loc = "upper left") plt.show()
The output is:
Multiple submaps in one drawing
If you do not declare the artboard in advance, the default is to create one artboard and one drawing paper. So, how do you lot use Python to draw multiple sub-pictures on one artboard?
• Use the figure ()
method to create a drawing lath 1
• Use the subplot ()
method to create a drawing paper, and select the electric current drawing newspaper and draw, parameter 1 number of rows, parameter two number of columns, parameter iii selects the drawing paper, such as: ax1 = fig.add_subplot(221)
• Too use the subplot ()
method to select the drawing paper and draw
• Finally brandish graphics
1 2 3 iv five half dozen 7 8 9 x 11 12 13 14 15 16 17
import matplotlib.pyplot as plt import numpy every bit np fig = plt.figure() ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224) ten = np.linspace(0, np.pi) y2 = -x * 2 y_sin = np.sin(10) y_cos = np.cos(x) z = ten ** 2 + x ax1.plot(x, y_cos) ax2.plot(x, z, 'co-', linewidth = 1, markersize = 2) ax3.plot(10, y_sin, color = 'blue', marking = '+', linestyle = 'dashed') ax4.plot(x, y2, 'k-.', markersize = 2) plt.show()
The output is:
Scatter plot
First, the points x and y of the data to be drawn are generated, and then an assortment size of control size is generated for each information point, so an array colour of control color is generated for each information betoken. Finally, a color bar is added through colorbar ()
.
1 2 3 4 5 half-dozen 7 8 ix
import matplotlib.pyplot equally plt import numpy as np fig = plt.figure() x = np.random.normal(1, five, 500) y = np.random.normal(1, 5, 500) colour = np.arctan2(x, y) plt.scatter(ten, y, s = 50, c = color, alpha = 0.eight) plt.colorbar() plt.prove()
The output is:
Histogram
We often apply histograms. Allow'south draw a histogram and marker the value corresponding to the data on the graph. Subsequently generating the data x and y, telephone call the plt.bar role to draw a histogram, and then mark the value with plt.text, set the parameter ha='center' to align horizontally to the eye, and set va='bottom' to marshal vertically to the lesser (top).
one 2 3 4 5 six vii 8 9 x 11
import matplotlib.pyplot as plt import numpy equally np x = np.arange(x) y = ten ** ii + 5 plt.bar(10, y, facecolor = 'c', edgecolor = 'westward') plt.bar(x, -y, facecolor = 'm', edgecolor = 'w') for i, j in nix(x, y): plt.text(i, j, "{f}".format(f = j), ha = "middle", va = 'bottom') for i, j in zip(10, y): plt.text(i, -j, "{f}".format(f = j), ha = "centre", va = 'top') plt.show()
The output is:
Pie nautical chart
The pie chart is drawn by directly calling the pie method of plt.
explode
: the per centum of a block protruding from the eye, 0 means not protruding, and 0.1 means protruding 0.1 from the center.
startangle
: tin can rotate the figure counterclockwise;
autopct='%one.1f%%'
: display the per centum of each cake, with one decimal place;
shadow
: when it is true, a "three-dimensional consequence" tin be created at the edges of certain blocks;
wedgeprops={'edgecolor':'black'}
is to set the color of the decomposition line between blocks.
i 2 3 4 five 6 7
import matplotlib.pyplot equally plt slices = [32, 25, 6, xviii, 12] labels = 'C', 'Java', 'C++', 'Python', 'Dart' plt.axes(aspect = 1) explode = [0, 0, 0, 0.i, 0] plt.pie(x = slices, labels = labels, machinepercent = '%1.1f %%', shadow = True, startangle = 120, explode = explode) plt.show()
The output is:
The to a higher place introduces several commonly used two-dimensional graphics drawn with matplotlib, and we will introduce matplotlib to draw three-dimensional graphics in another article.
cooperhichislon75.blogspot.com
Source: https://www.topcoder.com/thrive/articles/draw-different-two-dimensional-graphs-with-matplotlib-in-python
0 Response to "How to Draw a 2d Plane in Python"
Enviar um comentário