Python matplotlibでの誤差棒の表示
Pythonのmatplotlibで誤差棒を表示したいのですが,誤差が値yの上下で違う場合はどうすればいいのでしょうか
以下のサイトを参考にプログラムを試しに描いてみたのですがエラーが出てしまいます.
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q14182586664
エラーメッセージ
Traceback (most recent call last): File "test.py", line 25, in <module> plt.errorbar(x,y,yerr,label="", fmt='ro', ecolor='g') File "/Users/yaco/.pyenv/versions/3.5.1/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2930, in errorbar **kwargs) File "/Users/yaco/.pyenv/versions/3.5.1/lib/python3.5/site-packages/matplotlib/__init__.py", line 1892, in inner return func(ax, *args, **kwargs) File "/Users/yaco/.pyenv/versions/3.5.1/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 3020, in errorbar lower, upper = extract_err(yerr, y) File "/Users/yaco/.pyenv/versions/3.5.1/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 2961, in extract_err raise ValueError("err must be [ scalar | N, Nx1 " ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ]
ソースコード
python
1# coding: UTF-8 2import matplotlib 3import matplotlib.pyplot as plt 4import pandas as pd 5import numpy as np 6import datetime as dt 7 8plt.style.use('seaborn-whitegrid') 9plt.style.use('seaborn-colorblind') 10font = {'family' : 'meiryo'} 11matplotlib.rc('font', **font) 12 13x = np.array([l for l in range(5)], dtype=int) 14x_label = ['30~39.9%', '40~49.9%', '50~59.9%', '60~69.9%', '70~80%'] 15 16#グラフを描く 17x = [1,2,3,4,5] 18y = [10,20,30,40,50] 19yerr = [[2,4],[1,3],[3,4],[3,2],[2,4]] 20 21#誤差棒を描く 22plt.errorbar(x,y,yerr,label="", fmt='ro', ecolor='g') 23plt.bar(x, y, color="dodgerblue", label="glpk", tick_label=x_label, align="center") 24 25plt.legend() 26plt.xlabel("X軸") 27plt.ylabel("Y軸") 28plt.title("図 1.1") 29plt.axis([0,5,0,100]) 30plt.savefig("test.eps")
yerrの値の書き方がおかしい気がするのですが,どこのサイトもyの誤差の値が同じ形でplotしてるので,,どう書けばいいでしょうか
回答2件
あなたの回答
tips
プレビュー