matplotlib 初歩的な使い方
またにしか使わず、すぐ忘れてしまうので備忘メモ。
目次
グラフ(log(x))を表示(線)
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) plt.plot(x, np.log(x)) plt.show() if __name__ == '__main__': main()
グラフ(log(x))を表示(点)
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) plt.scatter(x, np.log(x)) plt.show() if __name__ == '__main__': main()
ラベルを設定
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) plt.plot(x, np.log(x)) plt.title('y = log(x)') plt.xlabel('x') plt.ylabel('y') plt.show() if __name__ == '__main__': main()
表示範囲を指定
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) plt.plot(x, np.log(x)) plt.xlim(0.0, 5.0) plt.ylim(0.0, 2.0) plt.show() if __name__ == '__main__': main()
補助線、目盛りの表示
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) plt.plot(x, np.log(x)) plt.axvline(1.0, color='Y') plt.axhline(2.0, color='B') plt.xticks([2.5, 3.0, 3.5]) plt.yticks([0.5, 1.0, 1.5]) plt.show() if __name__ == '__main__': main()
グリッドの表示
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) ax = plt.gca() ax.xaxis.grid(True) ax.yaxis.grid(True) plt.show() if __name__ == '__main__': main()
凡例を表示
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) plt.scatter(x, np.log(x), label='log(x)') plt.legend(loc='upper left') plt.show() if __name__ == '__main__': main()
分割表示
import numpy as np import matplotlib.pyplot as plt def main(): x = np.arange(-10, 10, 0.1) fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 4)) fig.suptitle('title') ax[0].plot(x, np.log(x)) ax[1].scatter(x, np.log(x)) plt.show() if __name__ == '__main__': main()
コメント
コメントを投稿