gpt4 book ai didi

python - matplotlib - 具有不同基指数和图例的辅助 Y 轴

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:14 25 4
gpt4 key购买 nike

所以我现在开始使用 matplotlib 处理更复杂的图形,并尝试使用以下绘图和代码。

enter image description here

fig_base = pylab.figure()
fig1 = fig_base.add_subplot(111)
lns1 = fig1.plot(manXnames, Ynames, marker='s', color='g')
lns2 = fig1.plot(Vt_X, Vt_Y, marker='^', color='r')

# yticks on left
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs*1e3))

#axis labels
pylab.xlabel('Temperature (C)')
pylab.ylabel('Emitter Voltage (mV)', labelpad=20)
pylab.xticks(rotation=45)

fig2 = fig1.twinx()
lns3 = fig2.plot(manXnames, Vterror, marker='o', linestyle='-')

# xticks
locs,labels = xticks()
pylab.xticks(locs, map(lambda x: "%g" % x, locs))

# yticks on right
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs))

#2nd axis labels
pylab.ylabel('Percentage Error %', labelpad=20)
pylab.show()

我的两个问题如下:

  1. 我认为我以一种奇怪的方式完成了基数指数修饰符,因为我有两个 y 轴。有没有更好的方法来绘制双轴图,因为我似乎只是通过将它直接放在轴的 .plot 之后来破解它。
  2. 另外,因为我有两个独立的轴,所以我无法让图例正常工作。

我尝试了以下来自 here 的类似问题答案

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10

fig = plt.figure()
ax = fig.add_subplot(111)

lns1 = ax.plot(time, Swdown, '-', label = 'Swdown')
lns2 = ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
lns3 = ax2.plot(time, temp, '-r', label = 'temp')

lns = lns1+lns2+lns3
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc=0)

ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
plt.show()

因此我的代码中有 lns。但每次我以这种方式运行时,Pylab 都会崩溃而不会报错。

任何人都可以帮助解决这些问题吗?

编辑:这是我在使用从 macduff 的回复中复制的完全相同的代码时遇到的错误,这与我提到的代码相同。

In [16]: te.test()

C:\Python27\lib\site-packages\matplotlib\legend.py:610: UserWarning: Legend does
not support [<matplotlib.lines.Line2D object at 0x04F8D410>]
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp:/
/matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str
(orig_handle),))
C:\Python27\lib\site-packages\matplotlib\legend.py:610: UserWarning: Legend does
not support [<matplotlib.lines.Line2D object at 0x04F8D630>]
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp:/
/matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str
(orig_handle),))
C:\Python27\lib\site-packages\matplotlib\legend.py:610: UserWarning: Legend does
not support [<matplotlib.lines.Line2D object at 0x04FB4D90>]
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp:/
/matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str
(orig_handle),))

最佳答案

因此,您似乎对图例未按您希望的方式工作感到不安。当我采用你的例程时,添加一些省略的测试数据;-),并添加一个传奇,我没有问题。让我知道如何帮助解决这个问题出。

import pylab
from pylab import *
import numpy as np

manXnames = np.array(range(0,120))
Ynames = (8.3333333333333331e-05)*manXnames + 0.01
Vt_X = manXnames
Vt_Y = (12.0/1000.0)*np.exp(-0.01*(120-manXnames))
Vterror = Ynames + randn(size(Ynames))

fig_base = pylab.figure()
fig1 = fig_base.add_subplot(111)
lns1 = fig1.plot(manXnames, Ynames, marker='s', color='g',label='Plain Line')
lns2 = fig1.plot(Vt_X, Vt_Y, marker='^', color='r',label='V_t')

# yticks on left
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs*1e3))

#axis labels
pylab.xlabel('Temperature (C)')
pylab.ylabel('Emitter Voltage (mV)', labelpad=20)
pylab.xticks(rotation=45)

fig2 = fig1.twinx()
lns3 = fig2.plot(manXnames, Vterror, marker='o', linestyle='-',label='V_terror')

# xticks
locs,labels = xticks()
pylab.xticks(locs, map(lambda x: "%g" % x, locs))

# yticks on right
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs))

#2nd axis labels
pylab.ylabel('Percentage Error %', labelpad=20)

pylab.legend((lns1, lns2, lns3), ('Plain Line', 'V_t', 'V_t Error'))
pylab.show()

稍后我会添加我的输出图。

关于python - matplotlib - 具有不同基指数和图例的辅助 Y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9528014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com