2つのリストxとyを使用してグラフをプロットしたいここで-
x=['18ww25', '18ww27', '18ww28', '18ww28.1', '18ww29', '18ww29.1', '18ww29.2']
y=[['27%', '27%', '27%', '27%', '27%', '27%', '27%'], ['18%', '18%', '20%', '23%', '30%', '30%', '30%'], ['24%', '25%', '28%', '32%', '39%', '39%', '43%'], ['43%', '43%', '44%', '44%', '43%', '45%', '45%'], ['0%', '0%', '0%', '0%', '0%', '0%', '0%'], ['14%', '14%', '16%', '16%', '16%', '17%', '17%'], ['14%', '14%', '14%', '14%', '14%', '14%', '14%'], ['0%', '0%', '0%', '0%', '0%', '0%', '0%'], ['8%', '8%', '8%', '8%', '8%', '8%', '8%']]
それぞれx
とy
ペアでグラフをプロットする必要があります。次のように、同じグラフにx
すべてのリストを含むリストをプロットする必要があります。... (合計9つの要素がにあるため)。y
(x,y1)
(x,y2)
(x,y9)
y
私が今使用しているコードは次のとおりです。
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(y)):
plt.plot(x,[pt[i] for pt in y],label = 'id %s'%i)
plt.legend()
plt.show()
しかし、このコードは次のようにエラーを示しています
x and y must have same first dimension, but have shapes (7,) and (9,)
どうすればこれをプロットできますか?
プロットしたいようです
for i in range(len(y)):
plt.plot(x, y[i], label = 'id %s'%i)
ただし、おそらく最初にy値を数値に変更します。そうしないと、縮尺どおりになりません。合計すると、次のようになります
import matplotlib.pyplot as plt
x=['18ww25', '18ww27', '18ww28', '18ww28.1', '18ww29', '18ww29.1', '18ww29.2']
y=[['27%', '27%', '27%', '27%', '27%', '27%', '27%'],
['18%', '18%', '20%', '23%', '30%', '30%', '30%'],
['24%', '25%', '28%', '32%', '39%', '39%', '43%'],
['43%', '43%', '44%', '44%', '43%', '45%', '45%'],
['0%', '0%', '0%', '0%', '0%', '0%', '0%'],
['14%', '14%', '16%', '16%', '16%', '17%', '17%'],
['14%', '14%', '14%', '14%', '14%', '14%', '14%'],
['0%', '0%', '0%', '0%', '0%', '0%', '0%'],
['8%', '8%', '8%', '8%', '8%', '8%', '8%']]
z = [list(map(lambda s: float(s.split("%")[0]),yi)) for yi in y]
plt.xlabel("X-axis")
plt.ylabel("Y-axis [%]")
plt.title("A test graph")
for i in range(len(z)):
plt.plot(x,z[i],label = 'id %s'%i)
plt.legend()
plt.show()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加