[Python] Matplotlib

Source: Internet
Author: User

#-*-Coding:utf-8-*-import unittestclass TestClass1 (unittest. TestCase): @unittest. Skip ("Core.ok") def test_tuple (self): data = (11,22,33) print (data) x, Y, Z = Data print (x, y, z) @unittest. Skip ("Matplotlib.screen") def Test_scrap_screen (self): from PIL import I        Mage, Imagegrab import NumPy as NP from matplotlib import Pyplot as Plt sim = Imagegrab.grab ()        W,h = Sim.size del (SIM) dpi = plt.rcparams[' figure.dpi '] print ("Screen size:%d,%d"% (w, h))         Print ("screen inch:%2.2f,%2.2f"% (w/dpi, h/dpi)) DJX = np.sqrt (w*w + h*h) print ("Screen DJX =%d"% DJX) Print ("screen dpi =%f"% (djx/15.4)) print ("Screen size=%d,%d"% (w/221, h/221)) # @unittest. Skip ("xx        Xyyyxxxyyyyy ") def Test_full_screen (self): from matplotlib import Pyplot as plt from PIL import Image Img_name = "/users/jacobzhao/downloads/sunyunzhu1.jpeg" img = Image.opeN (img_name) print ("dip =%d"% plt.rcparams[' figure.dpi ') plt.figure () Plt.axis (' off ') ax = P        LT.GCA () ax.spines[' top '].set_visible (False) leg = Plt.legend () leg.get_frame (). Set_linewidth (0.0) Plt.subplots_adjust (hspace=0, wspace=0) plt.imshow (IMG) plt.show () if __name__ = = ' __main__ ': unittes T.main () # suite = UnitTest. TestSuite () # suite.addtest (TestClass1 (' Test_scrap_screen ')) # runner = UnitTest. Texttestresult () # Runner.run (Suite)

  

#-*-Coding:utf-8-*-from functools import reduceimport numpy as Npimport matplotlib.pyplot as Pltfrom PIL import Imagec Lass MyImage (object): Def __init__ (self, filepath): Self.filepath = FilePath self.imgfile = Image.open (fi Lepath) if self.imgfile.mode! = ' RGB ': Self.imgfile = Self.imgfile.convert ("RGB") Self.imgdata =        Np.array (self.imgfile) self.info = {"mode": Self.imgfile.mode, "shape": self.imgdata.shape } def showimg (self, title= ' No-title '): Plt.figure (title) plt.imshow (self.imgdata) plt.axis (' O FF ') plt.show () def printinfo (self): print (Self.info) class MyImage2 (object): Def __init__ (self, Filepa Th1, filepath2): self.filepath1 = filepath1 Self.filepath2 = filepath2 self.imgfile1 = Image.open (fil epath1) Self.imgfile2 = Image.open (filepath2) if self.imgfile1.mode! = ' RGB ': self.imgfile1 = sel F.imgfile1.convert ("RGB"If self.imgfile2.mode! = ' RGB ': Self.imgfile2 = Self.imgfile2.convert ("RGB") Self.imgdata1 = NP . Array (self.imgfile1) Self.imgdata2 = Np.array (self.imgfile2) self.info1 = {"mode": Self.imgfile            1.mode, "Shape": self.imgdata1.shape} Self.info2 = {"mode": Self.imgfile2.mode, "Shape": Self.imgdata2.shape} def showimg (self, title1= ' Image-1 ', title2= ' Image-2 '): Fig = Plt.figu Re (figsize= (5)) Ax1 = Fig.add_subplot (1, 2, 1) ax1.imshow (self.imgdata1) ax1.axis (' off ') p Lt.title (title1) ax2 = Fig.add_subplot (1, 2, 2) ax2.imshow (SELF.IMGDATA2) ax2.axis (' off ') plt.        Title (title2) # plt.suptitle ("Image-test") plt.show () def printinfo (self): print (SELF.INFO1) Print (Self.info2) class Myimagen (object): Def __init__ (self, filepaths): Self.size = Len (filepaths) POS = List (range (self.size))        Self.filepaths = filepaths Self.imgfiles = [] Self.imgdatas = [] Self.imginfos = [] fo R index, filepath in Zip (POS, filepaths): info={' title ': ' img-' +str (Index)} self.imgfiles.append (Imag E.open (Filepaths[index]) info[' mode ' = Self.imgfiles[index].mode if self.imgfiles[index].mode! = ' RGB ': self.imgfiles[index] = Self.imgfiles[index].convert (' RGB ') self.imgdatas.append (Np.array (s Elf.imgfiles[index]) info[' shape ' = self.imgdatas[index].shape self.imginfos.append (info) def s            Howimg (self, titles = None): Fig = plt.figure (figsize= (4*self.size, 4)) for index in range (self.size):            Ax = Fig.add_subplot (1, self.size, index + 1) ax.imshow (Self.imgdatas[index]) ax.axis (' off ')        Plt.title ("image-" +str (Index)) # Plt.suptitle ("Image-test") plt.show () def printinfos (self): For index in range (Self.size): Print (index+1, '-a ', Self.imginfos[index]) def processimage (self, Index, func): Curr_d ATA = Self.imgdatas[index] Self.imgdatas[index] = func (Self.imgfiles[index]) def picgray (imgfile): Return imgfile . Convert (' L ') if __name__ = = ' __main__ ': Syz = [R '/users/jacobzhao/downloads/sunyunzhu1.jpeg ', R '/users/j Acobzhao/downloads/sunyunzhu2.png ", # r"/users/jacobzhao/downloads/sunyunzhu3.png ",] # myimage (syz[0]). ShowI    MG () # myimage (Syz[1]). showimg () Syzimgs = Myimagen (syz) # syzimgs.showimg () syzimgs.processimage (0, Picgray) Syzimgs.processimage (1, Picgray) # syzimgs.processimage (2, Picgray) Syzimgs.printinfos () syzimgs.showimg ()

  

#-*-Coding:utf-8-*-import Matplotlib.pyplot as Pltimport numpy as Npfig = Plt.figure (figsize= (8, 4)) Ax1 = FIG.ADD_SUBP Lot (2,2,1) ax2 = Fig.add_subplot (2,2,2) ax3 = Fig.add_subplot (2,1,2) Ax3.plot (Np.random.randn (). Cumsum (), ' k--') Ax1.hist (NP.RANDOM.RANDN (+), bins=10, color= ' B ', alpha=0.3) Ax2.scatter (Np.arange (+), Np.arange (30) + * NP.RANDOM.RANDN (+)) plt.show () a = [x+11 for x in range (6)]b = [y+101 for y in range (6)]print (A, b) for m,n in Zip (b): 
   
    print (M, n) Print (list (range (8)))
   

  

[Python] Matplotlib

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.