在子图中绘制平均线
Draw average line in subplot
我想在我的条形图子图中画一条平均线。我只用六个数据集来简化我的案例。对于这些值,我创建了平均值,这很容易(为了简短起见,此处未在我的示例中显示)。
但我找不到在我的条形图中集成一条线的方法,该线显示每个条的平均值。
import tkinter as Tk
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
root = Tk.Tk()
f = Figure(figsize=(5,4), dpi=100)
ax = f.add_subplot(111)
high = (104, 109, 113, 111, 108, 114)
low = (95, 100, 109, 103, 103, 110)
ind = np.arange(6) # the x locations for the groups
width = 0.35
rects1 = ax.bar(ind, high, width)
rects2 = ax.bar(ind, low, width)
ax.set_yticks(np.arange(90, 121, 2))
ax.set_ylim(bottom=90)
ax.legend((rects1[0], rects2[0]), ('High', 'Low'))
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
Tk.mainloop()
您可以使用列表推导来首先计算每组最低和最高柱的平均值。然后你可以使用虚线绘制它,例如如下所示
average = [(h+l)/2. for h, l in zip(high, low)]
rects1 = ax.bar(ind, high, width)
rects2 = ax.bar(ind, low, width)
ax.plot(ind, average, '--k')
我想在我的条形图子图中画一条平均线。我只用六个数据集来简化我的案例。对于这些值,我创建了平均值,这很容易(为了简短起见,此处未在我的示例中显示)。
但我找不到在我的条形图中集成一条线的方法,该线显示每个条的平均值。
import tkinter as Tk
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
root = Tk.Tk()
f = Figure(figsize=(5,4), dpi=100)
ax = f.add_subplot(111)
high = (104, 109, 113, 111, 108, 114)
low = (95, 100, 109, 103, 103, 110)
ind = np.arange(6) # the x locations for the groups
width = 0.35
rects1 = ax.bar(ind, high, width)
rects2 = ax.bar(ind, low, width)
ax.set_yticks(np.arange(90, 121, 2))
ax.set_ylim(bottom=90)
ax.legend((rects1[0], rects2[0]), ('High', 'Low'))
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
Tk.mainloop()
您可以使用列表推导来首先计算每组最低和最高柱的平均值。然后你可以使用虚线绘制它,例如如下所示
average = [(h+l)/2. for h, l in zip(high, low)]
rects1 = ax.bar(ind, high, width)
rects2 = ax.bar(ind, low, width)
ax.plot(ind, average, '--k')