我正试图使用matplotlib开发一个相当通用的图表,但始终得到一个错误的ValueError: left cannot be >= right。我的代码是:
def perf_plot(x, y, data, title = ''):
import matplotlib.pyplot as plt
import statsmodels.api as sm
plt.style.use('ggplot')
df = data
if y == 'slope':
z = 'bp01'
else:
z = 'slope'
y = df[y].astype(float)
x = df[x].astype(float)
z = df[z].astype(float)
blue = '#348EA9'
orange = '#F48B37'
green = '#52BA9B'
red = '#EF4846'
fig, ax1 = plt.subplots()
fig.suptitle(title, fontsize=14, fontweight='bold')
fig.subplots_adjust(top=0.85)
ax1.scatter(x ,y, color = orange)
lowess_y = sm.nonparametric.lowess(y, x ,frac=0.1)
ax1.plot(lowess_y[:, 0], lowess_y[:, 1], color = blue)
ax1.set_ylabel(y, color = blue)
ax1.set_xlabel(x, color = 'b')
ax2 = ax1.twinx()
lowess_z = sm.nonparametric.lowess(z, x, frac=0.1)
ax2.plot(lowess_z[:, 0], lowess_z[:, 1], color = green)
ax2.set_ylabel(z, color = green)
fig.tight_layout()
plt.show()
return在googling之后,我发现这个错误通常与tight_layout()和标题的使用相关。移除标题并不能解决问题,但是如果我删除了tight_layout,图形就会打印,但两个轴都放在图形的左侧。因为重点是做一些像这,我不是真正的如何获得没有tight_layout的决斗轴。想法?
错误码:
perf_plot(x_, y_, df1)
Traceback (most recent call last):
File "<ipython-input-149-36ff57804093>", line 1, in <module>
perf_plot(x_, y_, df1)
File "<ipython-input-148-ef2f325b64f9>", line 37, in perf_plot
fig.tight_layout()
File "C:\Users\user\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\figure.py", line 1755, in tight_layout
self.subplots_adjust(**kwargs)
File "C:\Users\user\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\figure.py", line 1620, in subplots_adjust
self.subplotpars.update(*args, **kwargs)
File "C:\Users\user\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\figure.py", line 228, in update
raise ValueError('left cannot be >= right')
ValueError: left cannot be >= right发布于 2017-11-28 16:49:11
这个错误是由
y = df[y].astype(float)
x = df[x].astype(float)
z = df[z].astype(float)哪里
ax1.set_ylabel(y, color = blue)
ax1.set_xlabel(x, color = 'b')导致标签成为数据格式。解决这一问题的办法是通过以下方式大幅增加数字大小:
fig, ax1 = plt.subplots(figsize=(8, 8)https://stackoverflow.com/questions/47536089
复制相似问题