First, draw a simple line chart
Import Matplotlib.pyplot as Pltsquares=[1,4,9,16,25]plt.plot (squares) plt.show ()
We first import the module Pylot, assign him the alias Plt, then create the list, store the preceding squares, and then pass the list to the function plot (), which attempts to draw a meaningful graph from these numbers. Plot.show () opens the Matplotlib viewer and displays the drawing drawing.
Operation Result:
Second, modify the label text and line thickness
#Coding:utf-8ImportMatplotlib.pyplot as Pltsquares=[1,4,9,16,25]plt.plot (Squares,linewidth= 5)#set the title of the graphic and label the axesPlt.title ("Squares Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.ylabel ("Square of Value", fontsize=14)#to set the size of a scale table markerPlt.tick_params (axis="both", labelsize=14) Plt.plot (squares) plt.show ()
Title (): Assign a caption to a chart
Xlabel (): Set the title for the X axis
Ylabel (): Sets the title for the y-axis
Tick_params (): Sets the style of the scale
Operation Result:
Correcting graphics
From the above graph can be seen: the end of the line graph 4 corresponds to the square of 25, the following to fix the problem
When you provide a series of numbers to plot (), it assumes that the first data point corresponds to the X axis value of 0, but our first axis corresponds to the x axis of 1, in order to change this default behavior, we can provide the input value and output value to plot ()
#Coding:utf-8ImportMatplotlib.pyplot as Pltinput_value= [1,2,3,4,5]squares=[1,4,9,16,25]plt.plot (Input_value,squares,linewidth= 5)#set the title of the graphic and label the axesPlt.title ("Squares Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.ylabel ("Square of Value", fontsize=14)#to set the size of a scale table markerPlt.tick_params (axis="both", labelsize=14) plt.show ()
Operation Result:
"Python" Matplotlib draw a line chart