Following on from Yesterday, I will explain the functions of matplotlib.
import numpy as np
from pandas import *
from pylab import *
import matplotlib.pyplot as plt
from matplotlib import font_manager
from numpy.random import randn
prop = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/fonts-japanese-gothic.ttf")
r = randn(30).cumsum()
#Specify color, linetype, marker
#Black, dashed line, marker is o
plt.plot(r, color='k', linestyle='dashed', marker='o')
plt.show()
plt.savefig("image.png ")
Lines are connected by a straight line by default. You can change this with the drawstyle option.
#Explicit RGB value
plt.plot(r, color='#ff0000', linestyle='dashed', marker='o', label='dashed')
#Change drawstyle
plt.plot(r, color='#0000ff', drawstyle='steps-post', label='steps-post')
#Add a legend
plt.legend(loc='best')
plt.show()
plt.savefig("image2.png ")
plt.xlim () and plt.xticks () return the current value when called with no arguments. Parameters can be set by specifying a value as an argument to this.
#Check the current value with no arguments
print( plt.xlim() )
# => (0.0, 30.0)
print( plt.xticks() )
# => (array([ 0., 5., 10., 15., 20., 25., 30.]), <a list of 7 Text xticklabel objects>)
#Set a new value
plt.xlim([0, 40])
plt.xticks([0,4,8,12,16,20,24,28,32,36,40])
print( plt.ylim() )
# => (-7.0, 3.0)
print( plt.yticks() )
# => (array([-8., -6., -4., -2., 0., 2., 4.]), <a list of 7 Text yticklabel objects>)
plt.ylim([-10, 10])
plt.yticks([-10,-8,-6,-4,-2,0,2,4,6,8,10])
plt.show()
plt.savefig("image3.png ")
Consider the following random walk plot.
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
r = randn(1000).cumsum()
ax.plot(r)
plt.show()
plt.savefig("image4.png ")
Let's customize the scale and label of this. For example, the X-axis is ranked by 250, the characters are tilted 30 degrees, and they are displayed in Japanese.
ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['A', 'B', 'C', 'D', 'E'], rotation=30, fontsize='small')
ax.set_title('Test matplotlib plot', fontproperties=prop)
ax.set_xlabel('Rank', fontproperties=prop)
plt.show()
plt.savefig("image5.png ")
An easy way to identify the plotted data is to label it and display it in a legend. It will be easier to understand if you separate the colors and line types for each data.
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def randn1000():
return randn(1000).cumsum()
ax.plot(randn1000(), 'k', label='one')
ax.plot(randn1000(), 'b--', label='two')
ax.plot(randn1000(), 'r.', label='three')
ax.plot(randn1000(), 'g+', label='four')
ax.plot(randn1000(), 'b*', label='five')
plt.ylim([-100, 100])
ax.legend(loc='best')
plt.show()
plt.savefig("image6.png ")
You can specify image file options with plt.savefig ().
argument | Description |
---|---|
fname | The character string containing the file path, the Python file object, and the format are automatically determined from the extension. |
dpi | The number of dots per inch and the resolution of the figure. The default is 100. |
facecolor,edgecolor | Background color outside the subplot. The default is w(White) 。 |
format | When you want to specify the file format explicitly. png,pdf etc. |
bbox_inches | Specify the part to save in the figure. Specifying tight removes the blank area around the figure. |
This time around, pandas hasn't appeared yet, so so far we're talking purely about matplotlib. From the next time, I will plot in combination with pandas.
Matplotlib usage notes http://www.geocities.jp/showa_yojyo/note/python-matplotlib.html
Introduction to data analysis with Python-Data processing using NumPy and pandas http://www.oreilly.co.jp/books/9784873116556/
Recommended Posts