Example of a matplotlib line chart in Python (axis numbers, string mashup, and title Chinese Display)

Source: Internet
Author: User

Recently in Python with the Matplotlib draw line chart, encountered the axis "digital + scale" mixed display, title Chinese display, batch processing and many other Problems. By learning to solve, to Record. If there are errors or deficiencies, please correct Me.

The simplest basic framework is as follows: known x, y, draw a line chart and Save. At this point both X and Y are Numbers.

1 #-*-coding:utf-8-*-2 3 ImportMatplotlib.pyplot as Plt#introduction of Matplotlib's Pyplot sub-library for drawing simple 2D graphs4 ImportRandom5 6x= Range (0,20)7y= [random.randint (0,20) for_inchRange (20)]8 9 #Build ObjectsTenFig = plt.figure (figsize= (8,6)) oneAx =Fig.add_subplot () a  - #Drawing -Plt.plot (x, y,'o', Label=u"Lines")#Drawing the plt.show () -Plt.savefig ("Temp.png")

second, the axis to increase the letter Elements:

Use the following statements and functions "reference: http://matplotlib.org/examples/ticks_and_spines/tick_labels_from_values.html":

From matplotlib.ticker import funcformatter, maxnlocator

Labels = list(' abcdefghijklmnopqrstuvwxyz ')

def FORMAT_FN (tick_val, tick_pos):
if int (tick_val) in xs:
Return labels[int (tick_val)]
Else
Return '

Ax. Xaxis. Set_major_formatter(funcformatter(format_fn))

Ax. Xaxis. Set_major_locator(maxnlocator(integer=True))

Slightly changed, used in the previous program:

1 #-*-coding:utf-8-*-2 3 ImportMatplotlib.pyplot as Plt#introduction of Matplotlib's Pyplot sub-library for drawing simple 2D graphs4 5  fromMatplotlib.tickerImportfuncformatter, Maxnlocator6 7 ImportRandom8 9x= Range (20) Ten  oney= [random.randint (0,20) for_inchRange (20)] a  -Fig = plt.figure (figsize= (8,6)) -Ax = Fig.add_subplot (111)#Build Objects the  -Labels = [1,2,3,4,5,6,7,8,9,10,'a','b','cc','D','e','F','g','h','*%$',' -'] -  -  + defformat_fn (tick_val, tick_pos): -     ifint (tick_val)inchX: +         returnLabels[int (tick_val)] a     Else: at         return "' -  - ax.xaxis.set_major_formatter (funcformatter (FORMAT_FN)) -Ax.xaxis.set_major_locator (maxnlocator (integer=True)) -  -Plt.plot (x, y,'o', Label=u"Lines")#Drawing in plt.show () -Plt.savefig ("Temp.png")

The axes can contain both strings and Numbers.

third, the title and other Chinese display:

The following libraries and statements are used:

From Matplotlib.font_manager import fontproperties

Font1 = fontproperties (fname=r "c:\windows\fonts\simsun.ttc", size=8) #可指定计算机内的任意字体, size is font

Plt.title (u "title", Fontproperties=font1)
Plt.xlabel (u "x axis)", fontproperties=font1)
Plt.ylabel (u "y-axis", Fontproperties=font1)
Ax.legend (prop=font1, loc= "upper Right")

This is used in the above program, you can display chinese:

#-*-coding:utf-8-*-ImportMatplotlib.pyplot as Plt#introduction of Matplotlib's Pyplot sub-library for drawing simple 2D graphs fromMatplotlib.tickerImportfuncformatter, MaxnlocatorImportRandom fromMatplotlib.font_managerImportFontpropertiesx= Range (20) y= [random.randint (0,20) for_inchRange (20)]fig= Plt.figure (figsize= (8,6) ) Ax= Fig.add_subplot (111)#Build ObjectsLabels= [1,2,3,4,5,6,7,8,9,10,'a','b','cc','D','e','F','g','h','*%$',' -']defformat_fn (tick_val, tick_pos):ifint (tick_val)inchX:returnLabels[int (tick_val)]Else:        return "'ax.xaxis.set_major_formatter (funcformatter (format_fn)) ax.xaxis.set_major_locator (maxnlocator (integer =True))#can specify any font in the computer, size is fontFont1 = Fontproperties (fname=r"C:\WINDOWS\FONTS\SIMSUN.TTC", size=20) Plt.title (u"title", fontproperties=font1) Plt.xlabel (u"X-axis", fontproperties=font1) Plt.ylabel (u"Y-axis", fontproperties=font1) plt.plot (x, y,'o', Label=u"Lines")#DrawingAx.legend (prop=font1, loc="Upper right") plt.show () plt.savefig ("Temp.png")


The drawing is as Follows:

four, do not display pictures, in order to batch Processing:

Import Matplotlib
Matplotlib.use (' Agg ')

To be added before import matplotlib.pyplot as plt, then delete plt.show () .

Example of a matplotlib line chart in Python (axis numbers, string mashup, and title Chinese Display)

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.