0

我想在 twinx() 多图中有误差线。这是我想到的一个最小的例子。

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y1 = 2.5 * np.sin(x / 20 * np.pi)
y2 = 2.5 * np.sin(x / 20 * np.pi)+1
xerr = np.linspace(0.05, 0.2, 10)
y2err = np.linspace(0.2, 0.4, 10)

fig, host = plt.subplots(figsize=(7,4.5))

par1 = host.twinx()

host.set_xlabel("blub")
host.set_ylabel("bla")
par1.set_ylabel("blablub")

p1, = host.plot(x, y1)
p2, = par1.errorbar(x, y2, xerr=xerr, yerr=y2err)

这里出现错误消息 “太多值无法解包(预期为 1)” 但第一个值已经被绘制

在我的真实程序中,情节如下所示(errorbarplot 为 p12):

if rise_fall[n]==1:
    markerdirection='->'
else:
    markerdirection='<-'

fig, host = plt.subplots(figsize=(16,9))

par1 = host.twinx()
par2 = host.twinx()


host.set_xlabel("Vgs/V")
host.set_ylabel("(Id/A)^1/2")
par1.set_ylabel("log(Id/A)")
par2.set_ylabel("slope(Id(Vgs))^2 / $\mu$A/V^2")


color1 = "red"
color2 = "blue"
color3 = "green"

p1, = host.plot(vgs_values, IDsqrt_values,  markerdirection, linewidth=0.75, color=color1, label="sqrt(Id) Measurement")
p2, = par1.plot(vgs_valuesLog, logID_values,  markerdirection, linewidth=0.75, color=color2, label="log(Id) Measurement")
p3, = par2.plot(vgs_LinSatArea, Slope_sqrtId, markerdirection, linewidth=0.5, markersize=4, color=color3, label="slope(Id(Vgs))^2")

p4, = host.plot(vgs_linFitSqrtIdPlot, Id_linSqrtFitPlot,  color="orange", label="lin Fit sqrt(Id)")
p5, = par1.plot(vgs_linFitLogIdPlot, logId_linFitPlot,  color="cyan", label="lin Fit log(Id)")

# host y=0 axis
p6, = host.plot([min(vgs_values), max(vgs_values)], [0,0], linestyle='dashed', linewidth=0.5, color='red')
# host lin Fit min
p7, = host.plot([vgs_values[LinFitSqrtIndMin], vgs_values[LinFitSqrtIndMin]], [max(IDsqrt_values),min(IDsqrt_values)], markerdirection, linestyle='dashed', linewidth=0.5, markersize=3, color='red')
# host lin Fit max
p8, = host.plot([vgs_values[LinFitSqrtIndMax], vgs_values[LinFitSqrtIndMax]], [max(IDsqrt_values),min(IDsqrt_values)], markerdirection, linestyle='dashed', linewidth=0.5, markersize=3, color='red')

# par1 y=offset voltage
p9, = par1.plot([min(vgs_values), max(vgs_values)], [SigFitParlogID[3],SigFitParlogID[3]], linestyle='dashed', linewidth=0.5, color='blue')
# par1 lin Fit min
p10, = par1.plot([vgs_values[LinFitLogIndMin], vgs_values[LinFitLogIndMin]], [max(logID_values),min(logID_values)], markerdirection, linestyle='dashed', linewidth=0.5, markersize=3, color='blue')
# par1 lin Fit max
p11, = par1.plot([vgs_values[LinFitLogIndMax], vgs_values[LinFitLogIndMax]], [max(logID_values),min(logID_values)], markerdirection, linestyle='dashed', linewidth=0.5, markersize=3, color='blue')

if MobilityExtraction: 
    p12, = host.errorbar(Vmax, sqrtImax, xerr=Vmax_uncer, yerr=sqrtImax_uncer, 'o', markersize=7, color='red', label="sqrt(Idmax), Vmax") 

lns = [p1, p2, p3, p4, p5]
labs = [l.get_label() for l in lns]
host.legend(lns, labs, loc='best')

# right, left, top, bottom
par2.spines['right'].set_position(('outward', 55))

host_t = host.secondary_xaxis('top')
#host_t.xaxis.label.set_color(p2.get_color())
#host_t.xaxis.label.set_visible(False)

host.yaxis.label.set_color(p1.get_color())
par1.yaxis.label.set_color(p2.get_color())
par2.yaxis.label.set_color(p3.get_color())

在这里我收到错误消息“位置参数遵循关键字参数”

有谁知道如何做到这一点以使其工作?

4

0 回答 0