Python matplotlib module is used to draw graphical charts. This article will just tell you how to use it to draw point and line. But before you can use it, you should make sure it is installed. You can open a terminal and input below command to check, if there is no error message print out, then matplotlib is installed.
1. Verify matplotlib Has Been Installed .
192:$ python Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import matplotlib >>>
2. Use matplotlib Draw Point Steps.
- First you should import matplotlib.pyplot module as below.
import matplotlib.pyplot as plt
- Then you can invoke pyplot.scatter method to draw a point or multiple points.
plt.scatter(3, 9, s=1000)
- You can also invoke pyplot other methods to draw x, y axis label and tick mark also.
2.1 Draw Single Point.
Run below example code in eclipse PyDev, there will popup matplotlib’s viewer that display below picture.
''' Created on Aug 23, 2018 @author: zhaosong ''' # Import pyplot module and alias it as plt. This can avoid repeatly call pyplot. import matplotlib.pyplot as plt # Draw a point based on the x, y axis value. def draw_point(): # Draw a point at the location (3, 9) with size 1000 plt.scatter(3, 9, s=1000) # Set chart title. plt.title("Square Numbers", fontsize=19) # Set x axis label. plt.xlabel("Number", fontsize=10) # Set y axis label. plt.ylabel("Square of Number", fontsize=10) # Set size of tick labels. plt.tick_params(axis='both', which='major', labelsize=9) # Display the plot in the matplotlib's viewer. plt.show() if __name__ == '__main__': draw_point()
2.2 Draw Multiple Points.
import matplotlib.pyplot as plt # Draw multiple points. def draw_multiple_points(): # x axis value list. x_number_list = [1, 4, 9, 16, 25] # y axis value list. y_number_list = [1, 2, 3, 4, 5] # Draw point based on above x, y axis values. plt.scatter(x_number_list, y_number_list, s=10) # Set chart title. plt.title("Extract Number Root ") # Set x, y label text. plt.xlabel("Number") plt.ylabel("Extract Root of Number") plt.show() if __name__ == '__main__': draw_multiple_points()
2.3 Draw Range Function Auto Generate Points.
This example draw multiple points use green color, and each point x, y axis is calculated with python range function automatically.
import matplotlib.pyplot as plt # Draw a serial of points which x, y axis value is calculated by range function. def draw_point_with_auto_generate_values(): # Set the x axis number max value. x_number_max = 100 # Auto generate x number value list by range function. x_number_list = list(range(1, x_number_max)) # Y axis value is assigned by x**3 y_number_list = [x**3 for x in x_number_list] # Draw points based on above x, y value list and remove the point black outline. And the point color is green. plt.scatter(x_number_list, y_number_list, s=10, edgecolors='none', c='green') # Set x, y axis min and max number. plt.axis([0, x_number_max, 0, x_number_max**3]) plt.show() if __name__ == '__main__': draw_point_with_auto_generate_values()
2.4 Save matplotlib Graphic As Image Programmaticaly.
If you click the disk button in the matplotlib viewer, you can save the graphic into an image. But you can also save it as image programmatically. You just need to call matplotlib.pyplot.savefig function.
# Import pyplot module and alias it as plt. This can avoid repeatly call pyplot. import matplotlib.pyplot as plt # Draw color point with color map and save result to a picture. def save_colorful_point_to_picture(): x_number_max = 100 x_number_list = list(range(1, x_number_max)) y_number_list = [x**2 for x in x_number_list] # Draw points which remove the point black outline. And the point color is changed with color map. plt.scatter(x_number_list, y_number_list, s=10, edgecolors='none', c=y_number_list, cmap=plt.cm.Reds) # Set x, y axis minimum and maximum number. plt.axis([0, x_number_max, 0, x_number_max**2]) #plt.show() # Save the plot result into a picture this time. plt.savefig('test_plot.png', bbox_inches='tight') if __name__ == '__main__': save_colorful_point_to_picture()
After run above code, you can find the test_plot.png file in current python execution path.
2. Use matplotlib Draw Line.
matplotlib.pyplot.plot function can be used to draw lines, please see below example.
import matplotlib.pyplot as plt # Plot a line based on the x and y axis value list. def draw_line(): # List to hold x values. x_number_values = [1, 2, 3, 4, 5] # List to hold y values. y_number_values = [1, 4, 9, 16, 25] # Plot the number in the list and set the line thickness. plt.plot(x_number_values, y_number_values, linewidth=3) # Set the line chart title and the text font size. plt.title("Square Numbers", fontsize=19) # Set x axes label. plt.xlabel("Number Value", fontsize=10) # Set y axes label. plt.ylabel("Square of Number", fontsize=10) # Set the x, y axis tick marks text size. plt.tick_params(axis='both', labelsize=9) # Display the plot in the matplotlib's viewer. plt.show() if __name__ == '__main__': draw_line()