Python development [module]: matplotlib draw line chart, pythonmatplotlib
Matplotlib
1. Install matplotlib
① Linux Installation
# Install the matplotlib module $ sudo apt-get install python3-matplotlib # If it is python2.7 execute the following command $ sudo apt-get install python-matplotlib # If you install newer Python, install modules some libraries $ sudo apt-get install python3.5-dev python3.5-tk-dev $ sudo apt-get install libfreetype6-dev g ++ # Use pip to install modules $ pip install -- user matplotlib
② Installation in OS
# Install the matplotlib module $ pip install -- user matplotlib
③ Installation in windows
# Install the matplotlib module $ pip3 install matplotlib
When you enter the terminal to execute import matplotlib, no error is reported, indicating that the execution is successful.
2. Draw a simple line chart
① Create the mpl_squares.py file:
Import matplotlib. pyplot as plt # import module squares = [, 25] # specify the Y coordinate of the list as the value, and the X coordinate as the subscript plt. plot (squares) # input list plt. show () # output image
Drawing:
② Modify the label text and line width:
Import matplotlib. pyplot as plt # import module squares = [, 25] # specify the Y coordinate of the list as the value, and the X coordinate as the subscript plt. plot (squares, linewidth = 5) # linewidth determines the drawing line width plt. title ('square Numbers ', fontsize = 24) # title plt. xlabel ('vaule', fontsize = 14) plt. ylabel ('square of vaule', fontsize = 14) plt. tick_params (axis = 'both ', labelsize = 14) # scale bold plt. show () # output image
Drawing:
③ Calibration image (set X coordinates ):
Import matplotlib. pyplot as plt # import module squares = [, 25] # specify the Y coordinate of the list as the value input_values = [, 5] plt. plot (input_values, squares, linewidth = 5) # linewidth determines the width of the drawn line plt. title ('square Numbers ', fontsize = 24) # title plt. xlabel ('vaule', fontsize = 14) plt. ylabel ('square of vaule', fontsize = 14) plt. tick_params (axis = 'both ', labelsize = 14) # scale bold plt. show () # output image
Drawing:
3. Draw a scatter chart
① Create scatter_sqares.py:
Import matplotlib. pyplot as pltplt. scatter (200, s = 200) # X coordinate 2, Y coordinate 4 S = point size plt. title ('square Numbers ', fontsize = 24) # title plt. xlabel ('vaule', fontsize = 14) plt. ylabel ('square of vaule', fontsize = 14) plt. tick_params (axis = 'both ', labelsize = 14) # scale bold plt. show () # output image
Drawing:
② Draw a series of points:
Import matplotlib. pyplot as pltx_values = [, 5] # specify the X axis y_values = [, 25] # specify the Y axis plt. scatter (x_values, y_values, s = 100) -- snip --- plt. show () # output image
Plotting
③ Automatic Data calculation:
Import matplotlib. pyplot as pltx_values = list (range () # specify the x axis y_values = [x ** 2 for x in x_values] # specify the Y axis plt. scatter (x_values, y_values, s = 1, edgecolors = 'None') # edgecolors deletes the data point contour plt. title ('square Numbers ', fontsize = 24) # title plt. xlabel ('vaule', fontsize = 14) plt. ylabel ('square of vaule', fontsize = 14) plt. axis ([,]) # sets the coordinate value range plt. tick_params (axis = 'both ', labelsize = 14) # scale bold plt. show () # output image
Drawing:
④ Custom color
To modify the color of a data point, you can pass parameter c to scatter () and set it to the name of the color to be used.
Plt. scatter (x_values, y_values, s = 1, edgecolors = 'none', c = 'red') # red
You can also use the RGB color mode to customize the color. to specify the custom color, input a ancestor to indicate the red, green, blue, and 0 ~ The value ranges from 1 to 0. The deeper the specified color, the closer the value is to 1, and the lighter the specified color.
Plt. scatter (x_values, y_values, s = 1, edgecolors = 'none', c = (0,0, 0.8) # Blue
Color ing-Gradient
Import matplotlib. pyplot as pltx_values = list (range () # specify the x axis y_values = [x ** 2 for x in x_values] # specify the Y axis plt. scatter (x_values, y_values, c = y_values, cmap = plt. cm. blues, s = 1, edgecolors = 'none',) # from light blue to dark blue plt. title ('square Numbers ', fontsize = 24) # title plt. xlabel ('vaule', fontsize = 14) plt. ylabel ('square of vaule', fontsize = 14) plt. axis ([,]) # sets the coordinate value range plt. tick_params (axis = 'both ', labelsize = 14) # scale bold plt. show () # output image
Drawing: