科学の箱

科学・IT・登山の話題

Python

pyplotでx軸のラベルを90度回転させる

投稿日:

pyplotでx軸にラベルを記載するとラベル文字数が長すぎるためにお互いにオーバーラップしてみにくい。

このよう場合にはpyplot.xticksを実行する際にrotationを指定すればよい。

Rotating custom tick labels — Matplotlib 3.1.1 documentation

rotationはmatplotlib.textで定義されている属性の一部である。ほかにも様々な属性を指定できる。

matplotlib.text

まずrotationを指定しない場合を見てみる。

import matplotlib.pyplot as plt
%matplotlib inline
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels)
plt.show()

次にrotationを指定する。

import matplotlib.pyplot as plt
%matplotlib inline
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')
plt.show()

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

janomeで品詞を解析する

  import sys import os from glob import glob from janome.tokenizer import Tokenizer def main(): …

no image

automated the boring – day 3

https://automatetheboringstuff.com/ 今回はまずは文字列操作から。とりあえず基本のところを押さえておく。 print("Hello there!\nHow …

no image

kaggle Titanic Tutorial – 3

DecitionTreeのパラメータを調整する。 まずはMaxDepthから from sklearn.model_selection import LeaveOneOut from sklearn. …

no image

tensorflowをpipでインストールするときのオプション

tensorflow環境を構築するときにはpipを利用すると簡単である。pipの簡単な使い方としてはinstallオプションを指定すればよい。 pip install tensoflow しかしこのオ …

no image

numpy.arange()を使って等差数列を生成する

等差数列を作るためにはnumpy.linspace()を使うと話をした。しかしlinspace()は要素数を指定するためにかえって混乱を招くことがある。そこで便利なのが公差を指定して配列を作ってくくれ …

2019年10月
« 9月   11月 »
 123456
78910111213
14151617181920
21222324252627
28293031  

side bar top



アーカイブ

カテゴリー