This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

The Matplotlib module is very powerful, and the PyPlot class provides users with scripting methods for quickly drawing common charts such as broken lines, bars, and scatter points. Matplotlib, meanwhile, relies on numerous low-level renderers such as Agg to render image data.

In order to make the image more beautiful, we always use matplotlib to apply color to the image.

In the previous study, there were two main ways to apply colors in charts:

  • Set the keyword of the color filling attribute: For example, plot, bar, hist, PI, Contour, etc., all provide the keyword of the facecolor/color/ Cmap attribute
  • Render NUMpy Data: Render numpy data into images using imshow()/pcolor methods

In this issue, we will learn about matplotlib module to draw color table, chart color application method properties, let’s go~

1. Imshow () draws the color table

  • Imshow () method overview

    Pyplot.imshow () renders numpy generated data into 2D images

    • Imshow () renders RGBA data or 2D scalar data into a precolor image
    • Imshow () can be achieved by cmap | vmin | vmax specifies the color of the output level
  • The imshow() method draws the color table

    • Import matplotlib. Pyplot library
    • Call numpy.random.randint() to generate vector data
    • Call Pyplot.imshow () to render the data into an image
    • A call to pylot.show() shows the image
    x = np.random.randint(1.100,size=(3.5))
    plt.imshow(x)
    plt.show()
    Copy the code

  • Render color levels can be changed using CMAP, Vmin, vmax

     plt.imshow(x,cmap="hot",vmin=10,vmax=210)
    Copy the code

  • You can call the text() method to populate each color text

    fig,ax = plt.subplots()
    
    x = np.random.randint(1.100,size=(7.7))
    
    ax.imshow(x,cmap="magma_r")
    
    for i in range(7) :for j in range(7):
            text = ax.text(j,i,x[i,j],ha="center",va="center",color="w")
    
    plt.show()
    Copy the code

2. Pcolormesh () draws a color table

  • Pcolor () method overview

    • The pcolor() method uses quadrilaterals to create an unconventional color grid
    • The pcolor() method renders slowly for large matrices
    • The pcolor () method only supports processing masks of x and y
  • Overview of the pcolorMesh () method

    • The pcolorMesh () method uses squares to create a colored mesh
    • The pcolorMesh () method is suitable for large matrix data
    • The pcolorMesh () method sets the mask element’s Facecolor to transparent so that you can see the difference using the edge color
  • The pcolor() method is used

    x = np.random.rand(6.10)
    
    plt.pcolor(x)
    
    plt.colorbar()
    
    plt.show()
    Copy the code

  • The pcolormesh() method is implemented

    x = np.random.rand(6.10)
    
    plt.pcolormesh(x,edgecolors="k")
    
    plt.colorbar()
    
    plt.show()
    Copy the code

3. Draw colored lines with hline() and vline()

  • Overview of hline() and vline() methods

    • The pyplot.hline(y,xmin,xmax) method draws a horizontal line
    • The pyplot.vline(x,ymin,ymax) method draws vertical lines
  • Hline () and vline() are used in practice

    • Call Pyplot.rcparams [‘axes.prop_cycle’].by_key()[‘color’] to get the color of the axes object
    • Call the pyplot.vline(), pyplot.hline() methods to draw vertical horizontal lines
    prop_cycle = plt.rcParams['axes.prop_cycle']
    colors = prop_cycle.by_key()['color']
    
    for i,color in enumerate(colors):
    
        plt.vlines(i,0.10,color=color)
        plt.hlines(i,0.10,color=color)
    
    plt.show()
    Copy the code

4. Colorbar draws a colorbar

  • Colorbar () method overview

    • Pylot. colorbar adds a colorbar to the chart
    • Colorbar can be used in Scatter, Contour, IMshow and PColorMesh
    • The colorbar is displayed vertically in the chart by default, and you can set the level through orientation
  • The colorbar() method is used

    data = np.arange(100).reshape(10.10)
    
    im = plt.imshow(data)
    
    plt.colorbar(im,orientation="horizontal")
    
    plt.show()
    Copy the code

5. Chart color attributes

  • Color property keyword

    • Cmap :RGBA color mapping table in the form of “color mapping table name _R”
    • Color: RGBA color tuple or list
    • Facecolor: Graphic color
    • Edgecolor: graphic border color
    • Color value form:
      • Words for colors: e.g. Red, “red”
      • Short for color words such as red “r”, yellow “Y”
      • RGB format: hexadecimal format, such as “# 88C999 “; (r,g,b) tuple form
      • You can go to the color list
  • A list of common chart color attributes

    methods The chart of cmap color facecolor edgecolor
    pyplot.hist() histogram x Square root x Square root
    pyplot.plot() The line chart x Square root x x
    pyplot.bar() A histogram x Square root Square root Square root
    pyplot.pie() The pie chart x Square root (colors) x Square root
    pyplot.scatter() A scatter diagram Square root ) (c) x Square root (edgecolors)
    pyplot.contour() Contour map Square root Square root (colors) x x
    pyplot.boxplot() Box figure x x x x
    pyplot.violinplot() Bass figure x x x x
    pyplot.imshow() Display DTA as image Square root x x x
    pyplot.pcolor() Color grid Square root Square root Square root Square root (edgecolors)

conclusion

In this installment, we summarize the methods and properties of color used in drawing diagrams in the Matplotlib module. How to display the data clearly, the charts often use color to help us distinguish.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time