In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Python+matplotlib how to achieve simple curve drawing", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python+matplotlib how to achieve simple curve drawing" bar!
First, install matplotlib
1) since anaconda is installed, you can open anaconda prompt directly, and then use the command pip install matplotlib to install it. It may be slow due to image problems. The second method is recommended.
2) visit https://pypi.org/project/matplotlib/#files and look for wheel files (files with a .whl extension) that match the Python version you are using. For example, the matplotlib-3.5.1-cp39-cp39-win_amd64.whl corresponding to the python3.9 version is placed in the directory G:\ develop\ python, (or the directory you created)
Open anaconda prompt and execute it with the command pip install G:\ develop\ python\ matplotlib-3.5.1-cp39-cp39-win_amd64.whl (make sure the directory is consistent).
Second, test matplotlib
Open anaconda prompt, first enter python, and then enter import matplotlib, as shown in the figure, there is no error message, it means that the system is installed successfully.
3. Draw a simple broken line import matplotlib.pyplot as plt # import module matplotlib.pyplot, and rename it pltsquares = [1pje 4pje 9pje 1625pc 1625pc 36pc] # define an array plt.plot (squares, linewidth=5) # set the chart title, and label the axis and parameter linewidth to determine the thickness of the drawn line plt.title ("Square Numbers", fontsize=24) # set the title and font size plt.xlabel ("Value", fontsize=14) # x axis label And font size plt.ylabel ("Square of Value", fontsize=14) # y axis label, and font size plt.tick_params (axis='both', labelsize=14) # sets the size of the scale mark, and the function tick_params () sets the style of the scale plt.show ()
This completes a simple line chart, which works as follows:
Note: if a warning appears in the red box in the figure during operation, you need to reset the Tools in spyder, as shown in the following figure:
Fourth, use scatter () to draw a scatter chart and set its style 1. To draw a single point
You can use the function scatter () and pass it a pair of x and y coordinates, which will draw a point at the specified location:
Import matplotlib.pyplot as plt # imports the module matplotlib.pyplot and renames it to pltplt.scatter (2,4, slots 200) # calls scatter () and uses the argument s to set the size of the point used when drawing, position 2 4plt.title ("Square Numbers", fontsize=24) # sets the title of the chart and labels the axes plt.xlabel ("Value", fontsize=14) plt.ylabel ("Square of Value", fontsize=14) # sets the size of the scale mark plt.tick_params (axis='both', which='major', labelsize=14) plt.show ()
2. to draw a series of points
To draw a series of points, you only need to give the coordinates of the series points. In the above code, we replace the 2pr 4 of plt.scatter (2pr 4, slots 200) with two sequences respectively.
Import matplotlib.pyplot as plt # Import module matplotlib.pyplot and rename it to pltx_values = [1, 2, 3, 4, 5] # sequence of X axis y_values = [1,3 y_values 6, 9, 12] # sequence of y axis plt.scatter (x_values, y_values, slots 100) # called scatter () And use the argument s to set the size of the point used in drawing plt.title ("series Numbers", fontsize=24) # set the title of the chart and label the axis plt.xlabel ("Value", fontsize=14) plt.ylabel ("Value", fontsize=14) # set the size of the scale mark plt.tick_params (axis='both', which='major', labelsize=14) plt.show ()
The running results are as follows:
3. Automatically calculate the data
Like the above manual input points, or series, are relatively slow processing, the following use of for loop instead of manual input.
You can first define x_values as a series of numbers with values in a certain range, such as 1-1000, and the corresponding y_values is also a sequence that is generated in a certain way (function). Therefore, you can modify the above code as follows:
Import matplotlib.pyplot as plt # imports the module matplotlib.pyplot and renames it to pltx_values = list (range (1, 1001)) # defines a sequence from 1 to 1000, and y_values = [x values 2 for x in x_values] # defines how the Y value is generated. Plt.scatter (x_values, y_values, sound4) # calls scatter () And use the argument s to set the size of the point used when drawing the graph plt.title ("series Numbers", fontsize=24) # set the title of the chart and label the axis plt.xlabel ("Value", fontsize=14) plt.ylabel ("Value", fontsize=14) # set the size of the scale mark plt.tick_params (axis='both', which='major', labelsize=10) plt.axis ([1Mague 1100mei 1100000]) # pay attention to the parameter plt.show () of axis
The running results are as follows:
The points in matplotlib default to blue points and black contours, as shown in the above three figures. Because there are more points in the last picture, they are connected together, like a curve. In order to distinguish different points, the points can be of different colors.
You only need to configure a few more parameters to delete the black outline and change the color of the point.
Plt.scatter (x_values, y_values, edgecolor='none', slots 40), where edgecolor='none' means to delete the black outline
By changing the color of the data point, you can pass the parameter c to scatter () and set it to the name of the color you want to use, as follows:
Plt.scatter (x_values, y_values, cased colors, edgecolor='none', colors 40) # changes the color to red.
Color mapping (colormap)
Color mapping is a series of colors that gradient from the start color to the end color. In visualization, color mapping is used to highlight the rules of data.
Plt.scatter (x_values, y_values, c=y_values, cmap=plt.cm.Blues,edgecolor='none', slots 40) # calls the scatter () parameter c to set to a list of y values, and uses the parameter cmap to tell pyplot which color mapping to use, # displays dots with lower y values as light blue, and displays dots with higher y values as dark blue
The specific operation results are as follows:
Note that to understand the mapping of all related colors, visit the official website, click Examples, scroll down to Color Examples, and then click colormaps_reference for reference.
4. Automatically save the chart
The method plt.show () is to display the chart
To have the program automatically save the chart to a file, call the plt.savefig () method
Plt.savefig ('scatter.png', bbox_inches='tight') # saved as a picture file of scatter.png
Thank you for your reading, the above is the content of "how to achieve simple curve drawing in Python+matplotlib". After the study of this article, I believe you have a deeper understanding of how to achieve simple curve drawing in Python+matplotlib, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.