"Python" Matplotlib draw a line chart

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.