0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

matplotlibでx軸の範囲調整をx_limを使い試みたが、x_limの引数がX軸リストのindexとして取得されてしまう

Posted at

もの凄い初歩的な部分で時間を食ったので自分への戒めとしてここに記そうと思う。
まず以下のような散布図を描いた。

import matplotlib.pyplot as plt

X = ["10","20","30"]
Y = ["0.001", "0.01", "0.1"]

fig, ax = plt.subplots(figsize=(6, 5))
ax.scatter(X,Y)

image.png

ここで、X軸の範囲を0~にしたく思い、Axes.set_xlimを使い軸の範囲調整を試みた。

import matplotlib.pyplot as plt

X = ["10","20","30"]
Y = ["0.001", "0.01", "0.1"]

fig, ax = plt.subplots(figsize=(6, 5))
ax.scatter(X,Y)
ax.set_xlim(left=0)

image.png
左端をX=0にしたかったのだが、X=10が来てしまっている。このleft=0がリストのインデックスを拾ってしまっているようだ。
早速だが原因はデータリストの要素が文字列だったためである。
CSVからデータを取り込んでパパっと処理しようって意気込みだったからか全く気づけなかった、、、
つまりこれで解決

import matplotlib.pyplot as plt

X = ["10","20","30"]
Y = ["0.001", "0.01", "0.1"]

X2 = [int(x) for x in X]
Y2 = [float(y) for y in Y]

fig, ax = plt.subplots(figsize=(6, 5))
ax.scatter(X2,Y2)
ax.set_xlim(left=0)

image.png

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?