Python drawing learning getting started, python drawing getting started
This article describes the basic method for drawing Python images. We will share this with you for your reference. The details are as follows:
Python: Use matplotlib to draw charts
A powerful class library named matplotlib is used to plot charts in python. It can produce high-quality 2D and 3D images. Record them first and then learn from them.
Matplotlib download and API manual address: http://sourceforge.net/projects/matplotlib/files/matplotlib/
Mathematical library numpy download and API manual address: http://www.scipy.org/Download
Several drawing examples are from the API manual:
1. Simplest diagram:
Code:
#!/usr/bin/env pythonimport matplotlib.pyplot as pltplt.plot([10, 20, 30])plt.xlabel('tiems')plt.ylabel('numbers')plt.show()
2. pie chart:
Code:
#!/usr/bin/env python# -*- coding: utf-8 -*-from pylab import *# make a square figure and axesfigure(1, figsize=(6,6))ax = axes([0.1, 0.1, 0.8, 0.8])labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'fracs = [15,30,45, 10]explode=(0, 0.05, 0, 0)pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})savefig('D:\\pie.png')show()
3. Use the numpy library function:
Code:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import numpy as npimport matplotlib. pyplot as pltt = np. arange (0.0, 1.01, 0.01) s = np. sin (2*2 * np. pi * t) plt. fill (t, s * np. exp (-5 * t), 'R') plt. grid (True) # Save as PDF or PNG or plt. savefig ('d: \ test.pdf ') plt. show ()