是否有一个库可以用来生成下面的图?
我得到的最接近的是生成 2 个单独的图,然后将它们转换为 numpy 数组图像,然后将它们相加。但我的解决方案中不会有这些传说。
import cv2
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10),'b')
ax.set_xlabel('A')
ax.set_ylabel('B')
ax.yaxis.set_label_position("right")
ax.spines['bottom'].set_color('blue')
ax.spines['right'].set_color('blue')
ax.xaxis.label.set_color('blue')
ax.yaxis.label.set_color('blue')
ax.tick_params(axis='x', colors='blue')
ax.tick_params(axis='y', colors='blue')
ax.tick_params(right=True, left=False, top=False, labelleft=False, labelright=True)
fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10),'r')
ax.set_xlabel('A')
ax.set_ylabel('B')
ax.xaxis.set_label_position("top")
ax.spines['top'].set_color('red')
ax.spines['left'].set_color('red')
ax.xaxis.label.set_color('red')
ax.yaxis.label.set_color('red')
ax.tick_params(axis='x', colors='red')
ax.tick_params(axis='y', colors='red')
ax.tick_params(left=True, right=False, bottom=False, top=True, labeltop=True, labelbottom=False)
fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
答案 0 :(得分:1)
您可以为此使用 twinx()
和 twiny()
fig = plt.figure()
ax1 = plt.subplot(111)
line1, = ax1.plot(range1, data1, 'r', label='data1-range1')
ax1.set_xlabel('range1')
ax1.set_ylabel('data1')
ax1.xaxis.label.set_color('red')
ax1.yaxis.label.set_color('red')
ax1.tick_params(axis='both', colors='red')
_temp = ax1.twinx()
ax2 = _temp.twiny()
ax2.set_xlabel('range2')
line2, = ax2.plot(range2, data2, 'b', label='data2-range2')
ax2.tick_params(axis='x', colors='blue')
ax2.xaxis.label.set_color('blue')
_temp.tick_params(axis='y', colors='blue')
_temp.set_ylabel('data2', rotation=-90, labelpad=10, color='blue')
ax2.spines['left'].set_color('red')
ax2.spines['top'].set_color('blue')
ax2.spines['bottom'].set_color('red')
ax2.spines['right'].set_color('blue')
plt.legend([line1, line2], ['range1-data', 'range2-data2'], loc='lower right')
编辑 1: 编辑代码以包含 spines
并为其他文本、刻度线和标签着色。
编辑 2: 还包括两个图的图例。