:私は以下の私のコードを入れているmatplotlibの> = 2.1で実行され、
import matplotlib.pyplot as plt
cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing",
"washing"]
fig, ax = plt.subplots()
ax.plot(activity, dog, label="dog")
ax.plot(activity, cat, label="cat")
ax.legend()
plt.show()
実行細かい質問からコードをして
を生成します
以前のバージョンのmatplotlibでは、axesは数値である必要があります。したがって、カテゴリを数値に変換してプロットし、それに応じてティックラベルを設定する必要があります。
import numpy as np
import matplotlib.pyplot as plt
cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing",
"washing"]
catu, cati = np.unique(cat, return_inverse=True)
dogu, dogi = np.unique(dog, return_inverse=True)
fig, ax = plt.subplots()
ax.plot(range(len(dog)), dogi, label="dog")
ax.plot(range(len(cat)), cati, label="cat")
ax.set_xticks(range(len(activity)))
ax.set_xticklabels(activity)
ax.set_yticks(range(len(catu)))
ax.set_yticklabels(catu)
ax.legend()
plt.show()
カテゴリ変数の折れ線グラフをプロットするにはどうすればよいですか? – pissall
質問のコードは、matplotlib 2.1の[このグラフ](https://i.stack.imgur.com/r2zTl.png)を生成します。以前のバージョンを使用している場合、文字列をプロットすることはできません。エラーが発生した場合は、質問に完全な誤り追跡を含める必要があります。 – ImportanceOfBeingErnest
ありがとう、私はバージョン2.1をインストールせずにそれを行うことができる方法はありますか? –