Memorandum @ Python OR Seminar: matplotlib

matplotlib

>>> import pylab as plt
>>> from pylab import * #Easy but not very good pattern

Data preparation

>>> x = plt.linspace(0, 2 * math.pi, 50) # 0-2*An array of 50 equal parts of the π interval
>>> y = plt.sin(x)

Display as a graph

>>> p = plt.plot(x, y)
>>> plt.show() # ipython --Not needed if you use pylab

option

>>> plt.plot(x, y, 'option')
String color
'b' Blue
'g' Green
'r' Red
'c' cyan
'm' Magenta
'y' yellow
'k' black
'w' White
String Plot line
'-' solid line
'--' Dashed line
'-.' Dashed line ---
':' dotted line
'.' point
',' pixel
'o' Round
'v' Down triangle
'^' Upward triangle
'<' Left-pointing triangle
'>' Right-pointing triangle
's' square
'p' pentagon
'*' Star
etc

Legend / Label

-- legend (): Legend (what the line shows) --xlabel (): x-axis label -- ylabel (): y-axis label

If you want to use Japanese, you have to specify the font in advance with Font Properties.

>>> plt.xlabel(u"abcdefg")
>>> plt.ylabel(u"fugafuga")
>>> plt.legend([p], ("hogehoge"))
#I want to use Japanese
>>> prop = matplotlib.font_manager.FontProperties(fname="Absolute path of font")
>>> plt.xlabel(u"Fuga Fuga", fontproperties=prop)

Arrange the graphs

When you want to arrange multiple graphs in one figure. Surprisingly convenient and usable.

--subplot (): Specify where to edit with the argument

>>> plt.subplot(221) #1st in 2 vertical rows and 2 horizontal rows
>>> plt.plot(x, sin(x))
>>> plt.subplot(222)
>>> plt.plot(x, sin(-x))

Save the created figure

It is useful when you put the created graph on a paper or a slide.

--savefig (): Argument is the file name to save --.jpg, .png, .bmp, .eps, .svg, .pdf etc. can be selected

>>> plt.savefig("hoge.png ", transparent=True (or False) #Make the background transparent (not)

other than that

Basically, the above should be enough. But sometimes I use the one below.

histogram

>>> data = plt.normal(5, 3, 500) #Average 5,Normal random number with standard deviation 3
>>> plt.hist(data, bins=30, color="red"); None

bar graph

>>> years = [str(i) for i in range(2005, 2014)]
>>> steps = plt.arange(len(years)) 
>>> sales = 100 + 40 * plt.rand(len(years))
>>> plt.bar(steps, sales, align="center", color="green")
>>> plt.xticks(steps, years); None

pie chart

>>> rate_data = [0.3, 0.4, 0.2, 0.1]
>>> lbl = ["O", "A", "B", "AB"]
>>> ex = (0, 0.05, 0, 0)
>>> plt.axis("equal")
>>> plt.pie(rate_data, explode=ex, startangle=90, shadow=True, autopct="%1.1f%%", labels=lbl); None

Image display

>>> imagedata = array([
			[1,1,1,1,1,1,1,1,1,1],
			[1,1,1,1,1,1,1,1,1,1],
			[1,1,0,1,1,1,1,0,1,1],
			[1,0,1,0,1,1,0,1,0,1],
			[1,1,1,1,1,1,1,1,1,1],
			[1,1,1,1,1,1,1,1,1,1],
			[1,0,1,1,1,1,1,1,0,1],
			[1,1,0,1,1,1,1,0,1,1],
			[1,1,1,0,0,0,0,1,1,1],
			[1,1,1,1,1,1,1,1,1,1]
])
>>> plt.imshow(imagedata)

fill

>>> y1 = sin(x) * cos(x)
>>> plt.plot(x, y1)
>>> y2 = -sin(x) * cos(x)
>>> plt.plot(x, y2)
>>> plt.fill_between(x, y1, y2, interpolate=True, alpha=0.3)

3D plot

#3D plot example
>>> from mpl_toolkits.mplot3d import Axes3D
>>> x3d = rand(100)
>>> y3d = rand(100)
>>> z3d = rand(100)
>>> fig = figure()
>>> ax = fig.add_subplot(111, projection="3d")
>>> ax.scatter3D(x3d, y3d, z3d)


Recommended Posts

Memorandum @ Python OR Seminar: matplotlib
Memorandum @ Python OR Seminar
Memorandum @ Python OR Seminar: Pulp
Memorandum @ Python OR Seminar: Pandas
Memorandum @ Python OR Seminar: scikit-learn
Matplotlib memorandum
Python memorandum
Python Memorandum 2
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
Python pathlib memorandum
#Python basics (#matplotlib)
My matplotlib (python)
Python memorandum [links]
Python> list> extend () or + =
Python memorandum numbering variables
python memorandum (sequential update)
A memorandum about matplotlib
Python from or import
python autotest or sniffer
Python memorandum (personal bookmark)
Heatmap with Python + matplotlib
Python basic memorandum part 2
[Python] Iterative processing_Personal memorandum
Installation of matplotlib (Python 3.3.2)
python memorandum super basic
matplotlib Graph-related memorandum (minimum)
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Use matplotlib on Ubuntu 12 & Python
Python 3.4 or later standard pip
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
[Python] SQLAlchemy error avoidance memorandum
Python application: data visualization # 2: matplotlib
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Python bitwise operator and OR
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
[python] Random number generation memorandum
Effective Python Learning Memorandum Day 4 [4/100]
Ruby's `` like Python. 2.6 or later
Python or and and operator trap
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Create plot animation with Python + Matplotlib
Python pywin32 (win32com) Excel operation memorandum