如何绘制双Y轴

时间:2020-07-06 02:53:10

标签: python matplotlib axis

我当前的代码是:

hours_hr_mean = np.array(hours_hr_mean)
hours_hr_std = np.array(hours_hr_std)
hours_spo2_mean = np.array(hours_spo2_mean)
hours_spo2_std = np.array(hours_spo2_std)

all_hours = [hours_hr_mean,hours_hr_std,hours_spo2_mean,hours_spo2_std]
for i in all_hours:
    where_are_nans = np.isnan(i)
    i[where_are_nans] = 0
x_val = []
for i in range(len(fours)):
    x_val.append(i)
    

fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('four hour period number')
ax1.set_ylabel('heart rate (bpm)', color = color)
ax1.errorbar(x_val, hours_hr_mean, hours_hr_std, color = color,linestyle = 'None', marker = 'o')
ax1.tick_params(axis='y', labelcolor=red)
ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('spo2 levels', color = color)
ax2.errorbar(x_val, hours_spo2_mean, hours_spo2_std, color = color, linestyle = 'None', marker = 'o')
ax2.tick_params(axis='y',labelcolor=color)


plt.title('mean and standard deviation for heart rate and spo2 low activity periods')
plt.show()

我正在追溯:

Traceback (most recent call last):
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\backends\backend_qt5.py", line 498, in _draw_idle
    self.draw()
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 393, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\figure.py", line 1736, in draw
    renderer, self, artists, self.suppressComposite)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2630, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\axis.py", line 1232, in draw
    tick.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\axis.py", line 297, in draw
    artist.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\text.py", line 703, in draw
    gc.set_foreground(textobj.get_color())
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 938, in set_foreground
    self._rgb = colors.to_rgba(fg)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\colors.py", line 185, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\colors.py", line 272, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4
Traceback (most recent call last):
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\backends\backend_qt5.py", line 498, in _draw_idle
    self.draw()
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 393, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\figure.py", line 1736, in draw
    renderer, self, artists, self.suppressComposite)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2630, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\axis.py", line 1232, in draw
    tick.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\axis.py", line 297, in draw
    artist.draw(renderer)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\text.py", line 703, in draw
    gc.set_foreground(textobj.get_color())
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 938, in set_foreground
    self._rgb = colors.to_rgba(fg)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\colors.py", line 185, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "C:\Users\chloe\anaconda3\lib\site-packages\matplotlib\colors.py", line 272, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

如何正确执行此操作和/或解决错误?

0 个答案:

没有答案