matplotlib
>>> import pylab as plt
>>> from pylab import * #Easy but not very good pattern
>>> x = plt.linspace(0, 2 * math.pi, 50) # 0-2*An array of 50 equal parts of the π interval
>>> y = plt.sin(x)
>>> p = plt.plot(x, y)
>>> plt.show() # ipython --Not needed if you use pylab
>>> 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 (): 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)
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))
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)
Basically, the above should be enough. But sometimes I use the one below.
>>> data = plt.normal(5, 3, 500) #Average 5,Normal random number with standard deviation 3
>>> plt.hist(data, bins=30, color="red"); None
>>> 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
>>> 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
>>> 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)
>>> 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 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