LoginSignup
0

More than 3 years have passed since last update.

馬鹿の一つ覚え: Matplotlib

Last updated at Posted at 2018-06-22

Matplotlibの基本的な使い方1

import numpy
import matplotlib
# matplotlib.use('PDF') # PDFに保存する場合
import matplotlib.pyplot
# %matplotlib inline    # Jupyter Notebook

x = numpy.arange(-4.0, 4.0, 0.1)
sinh = (numpy.exp(x) - numpy.exp((-1) * x)) / 2.0    # 双曲線正弦関数
cosh = (numpy.exp(x) + numpy.exp((-1) * x)) / 2.0    # 双曲線余弦関数

matplotlib.pyplot.plot(x, sinh, label= 'sinh(x)')
matplotlib.pyplot.plot(x, cosh, linestyle = '--', label = 'cosh(x)')
matplotlib.pyplot.title('y = sinh(x) and y = cosh(x)')
matplotlib.pyplot.xlabel('x')
matplotlib.pyplot.ylabel('y')
matplotlib.pyplot.legend()
matplotlib.pyplot.show()    # 表示
# matplotlib.pyplot.savefig('sinhcosh.pdf') # PDFに保存

Matplotlibの基本的な使い方2

import numpy
import matplotlib.pyplot
# %matplotlib inline

number_of_volunteers = [ 9, 10,  8, 12, 11, 10,  7,  6,  5,  5, \
                         8,  7,  7,  9,  8,  4,  8,  5,  6, 12, \
                         6,  6,  8, 10, 11,  6,  8, 11,  7,  7, \
                         6, 11,  7,  8, 10,  7,  5,  5,  6,     ]

n_volunteers = numpy.array(number_of_volunteers)

x = numpy.arange(0, len(number_of_volunteers), 1)

matplotlib.pyplot.xlim(0.1, 42)    # 横軸の0を表示させないために
matplotlib.pyplot.ylim(0, 15)
matplotlib.pyplot.xlabel('Horticulture work')
matplotlib.pyplot.ylabel('Number of volunteers') 
matplotlib.pyplot.plot((x + 1), n_volunteers, color = "green", linewidth = 2)
matplotlib.pyplot.savefig('participants.pdf')

x軸のラベルを回転させて表示

matplotlib.pyplot.xticks(rotation = 60)    # 反時計回りに60度回転

下側がはみ出してしまう場合の対応

matplotlib.pyplot.savefig('figure.pdf', bbox_inches = 'tight')

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0