0

我想让你弄清楚的问题是关于带有双 y 轴的 matplotlib 图上的坐标出现。首先是 Jupyter Notebook 上的代码,它用两条线和一个 y 轴绘制了一个图形(由于某些未知原因,我必须运行它两次才能使其正常工作)

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab

from IPython.display import display 
from IPython.core.display import display, HTML #display multiple output on a cell 
display(HTML("<style>.container { width:100% !important; }</style>")) # improve cells horizontal size
from IPython.core.interactiveshell import InteractiveShell # It saves you having to repeatedly type "Display"
InteractiveShell.ast_node_interactivity = "all"
%matplotlib notebook

x = np.arange(0, 10, 0.01)
y1 = np.sin(np.pi*x)/(np.pi*x)
y2 = abs(np.tan(0.1*np.pi*x))

plt.figure()
plt.plot(x, y1)
plt.plot(x, y2)
plt.ylim(0, 3)
plt.grid()
plt.show()

在此处输入图像描述

本图在图形的右下角提供了两条带有光标坐标的线。

以下代码

import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab

from IPython.display import display 
from IPython.core.display import display, HTML #display multiple output on a cell 
display(HTML("<style>.container { width:100% !important; }</style>")) # improve cells horizontal size
from IPython.core.interactiveshell import InteractiveShell # It saves you having to repeatedly type "Display"
InteractiveShell.ast_node_interactivity = "all"
%matplotlib notebook

x = np.arange(0, 10, 0.01)
y1 = np.sin(np.pi*x)/(np.pi*x)
y2 = abs(np.tan(0.1*np.pi*x))

# Create some mock data
fig, ax1 = plt.subplots()
plt.grid()
color = 'tab:red'
ax1.set_xlabel('Time (days from 24 February)')
ax1.set_ylabel('Death cases/Intensive care', color=color)
#ax1.set_xlim(0, 15)
#ax1.set_ylim(0, 900)
ax1.plot(x, y1, '-', color=color, label = 'Left hand scale')
ax1.tick_params(axis='y', labelcolor=color)
ax1.legend(loc = 'upper left')

ax2 = ax1.twinx()  

color = 'tab:blue'
ax2.set_ylabel('Total cases/currently positive', color=color)  # we already handled the x-label with ax1
ax2.plot(x, y2, '-', color=color, label = 'Right hand scale')
ax2.set_ylim(0, 20)
ax2.tick_params(axis='y', labelcolor=color)
ax2.legend(loc = 'lower right')

fig.tight_layout()  

plt.show()

显示下图

在此处输入图像描述

它显示了一个带有两个 y 刻度的图表,左侧一个红色,右侧一个蓝色。这里的问题是,在图片的左下角有与右侧比例相关的光标坐标,而与左侧无关。有没有办法同时显示两个比例?

4

1 回答 1

1

根据您的确切需求,mplcursors似乎很有帮助。Mplcursors 允许多种方式进行自定义,例如您可以将两个 y 值与当前 x 一起显示。或者您可以取消注释并仅在状态栏中写入。

hover=True当鼠标悬停在曲线上时,设置会不断显示绘制的值。默认情况下,仅在单击时显示值。

import matplotlib.pyplot as plt
import numpy as np
import mplcursors

# Create some test data
x = np.arange(0, 10, 0.01)
y1 = np.sin(np.pi * x) / (np.pi * x)
y2 = abs(np.tan(0.1 * np.pi * x))

fig, ax1 = plt.subplots()
plt.grid()
color = 'tab:red'
ax1.set_xlabel('Time (days from 24 February)')
ax1.set_ylabel('Death cases/Intensive care', color=color)
lines1 = ax1.plot(x, y1, '-', color=color, label='Left hand scale')
ax1.tick_params(axis='y', labelcolor=color)
ax1.legend(loc='upper left')

ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('Total cases/currently positive', color=color)  # we already handled the x-label with ax1
lines2 = ax2.plot(x, y2, '-', color=color, label='Right hand scale')
ax2.set_ylim(0, 20)
ax2.tick_params(axis='y', labelcolor=color)
ax2.legend(loc='lower right')

cursor1 = mplcursors.cursor(lines1, hover=True)
cursor2 = mplcursors.cursor(lines2, hover=True)

fig.tight_layout()
plt.show()

结果图

于 2020-03-10T19:21:34.283 回答