Twinx网格线的可见性

时间:2019-03-27 08:52:04

标签: python matplotlib

使用private float minScale = 1f; private float maxScale = 5f; private static float saveScale = 1f; public void setMinScale(float scale) { if (scale < 1.0f || scale > maxScale) throw new RuntimeException("minScale can't be lower than 1 or larger than maxScale(" + maxScale + ")"); else minScale = scale; } public void setMaxScale(float scale) { if (scale < 1.0f || scale < minScale) throw new RuntimeException("maxScale can't be lower than 1 or minScale(" + minScale + ")"); else minScale = scale; } private static final int NONE = 0; private static final int DRAG = 1; private static final int ZOOM = 2; private int mode = NONE; private Matrix matrix = new Matrix(); private ScaleGestureDetector mScaleDetector; private float[] m; private PointF last = new PointF(); private PointF start = new PointF(); private float right, bottom; //////////////////// private class ZoomOnTouchListeners implements OnTouchListener { public ZoomOnTouchListeners() { super(); m = new float[9]; mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); } @Override public boolean onTouch(View view, MotionEvent motionEvent) { mScaleDetector.onTouchEvent(motionEvent); matrix.getValues(m); float x = m[Matrix.MTRANS_X]; float y = m[Matrix.MTRANS_Y]; PointF curr = new PointF(motionEvent.getX(), motionEvent.getY()); switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: last.set(motionEvent.getX(), motionEvent.getY()); start.set(last); mode = DRAG; break; case MotionEvent.ACTION_UP: mode = NONE; break; case MotionEvent.ACTION_POINTER_DOWN: last.set(motionEvent.getX(), motionEvent.getY()); start.set(last); mode = ZOOM; break; case MotionEvent.ACTION_MOVE: if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) { float deltaX = curr.x - last.x;// x difference float deltaY = curr.y - last.y;// y difference if (y + deltaY > 0) deltaY = -y; else if (y + deltaY < -bottom) deltaY = -(y + bottom); if (x + deltaX > 0) deltaX = -x; else if (x + deltaX < -right) deltaX = -(x + right); matrix.postTranslate(deltaX, deltaY); last.set(curr.x, curr.y); } break; case MotionEvent.ACTION_POINTER_UP: mode = NONE; break; } FramePlayerView.this.setTransform(matrix); FramePlayerView.this.invalidate(); return true; } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScaleBegin(ScaleGestureDetector detector) { mode = ZOOM; return true; } @Override public boolean onScale(ScaleGestureDetector detector) { float mScaleFactor = detector.getScaleFactor(); float origScale = saveScale; saveScale *= mScaleFactor; if (saveScale > maxScale) { saveScale = maxScale; mScaleFactor = maxScale / origScale; } else if (saveScale < minScale) { saveScale = minScale; mScaleFactor = minScale / origScale; } right = getWidth() * saveScale - getWidth(); bottom = getHeight() * saveScale - getHeight(); if (0 <= getWidth() || 0 <= getHeight()) { matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY()); if (mScaleFactor < 1) { matrix.getValues(m); float x = m[Matrix.MTRANS_X]; float y = m[Matrix.MTRANS_Y]; if (mScaleFactor < 1) { if (0 < getWidth()) { if (y < -bottom) matrix.postTranslate(0, -(y + bottom)); else if (y > 0) matrix.postTranslate(0, -y); } else { if (x < -right) matrix.postTranslate(-(x + right), 0); else if (x > 0) matrix.postTranslate(-x, 0); } } } } else { matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY()); matrix.getValues(m); float x = m[Matrix.MTRANS_X]; float y = m[Matrix.MTRANS_Y]; if (mScaleFactor < 1) { if (x < -right) matrix.postTranslate(-(x + right), 0); else if (x > 0) matrix.postTranslate(-x, 0); if (y < -bottom) matrix.postTranslate(0, -(y + bottom)); else if (y > 0) matrix.postTranslate(0, -y); } } if (saveScale>1.0f){ TestNewPlayer.isZoooomed(true); } if (saveScale == 1.0f){ TestNewPlayer.isZoooomed(false); } return true; } } } 创建具有两个不同高度比例的重叠条形图时,无法设置“双”轴集的垂直网格线。但是水平线工作正常。关于如何解决这个问题有什么想法吗?

下面是一些示例代码,这些代码说明了我想做什么和不能做什么。如图所示,垂直网格线被Axes.twinx()的红色条隐藏,而我希望网格线在所有条中都可见。

ax2

Checkbox size in HTML/CSS

由于# Create figure and figure layout ax1 = plt.subplot() ax2 = ax1.twinx() # Example data x = [0, 1, 2, 3, 4, 5] h1 = [55, 63, 70, 84, 73, 93] h2 = [4, 5, 4, 7, 4, 3] # Plot bars h1_bars = ax1.bar(x, h1, width=0.6, color='darkblue') h2_bars = ax2.bar(x, h2, width=0.6, color='darkred') # Set y limits and grid visibility for ax, ylim in zip([ax1, ax2], [100, 10]): ax.set_ylim(0, ylim) ax.grid(True) 的垂直网格线未设置为可见而发生错误。可以通过设置ax2进行测试,在这种情况下,只有水平网格线。

我尝试了ax1.grid(False)ax1.xaxis.grid(True)ax1.yaxis.grid(True)ax2.xaxis.grid(True)的所有组合,但没有任何运气。在这个问题上的任何帮助深表感谢!

1 个答案:

答案 0 :(得分:2)

您可以还原ax1和ax2的角色,以便蓝色条位于ax2上,红色条位于ax1上。然后,您需要将双轴放置在背景中,并在图的另一侧勾选相应的y轴。

import matplotlib.pyplot as plt

# Create figure and figure layout
ax1 = plt.subplot()
ax2 = ax1.twinx()

# Example data
x = [0, 1, 2, 3, 4, 5]
h1 = [55, 63, 70, 84, 73, 93]
h2 = [4, 5, 4, 7, 4, 3]

# Plot bars
h1_bars = ax2.bar(x, h1, width=0.6, color='darkblue')
h2_bars = ax1.bar(x, h2, width=0.6, color='darkred')

# Set y limits and grid visibility
for ax, ylim in zip([ax1, ax2], [10, 100]):
    ax.set_ylim(0, ylim)
    ax.grid(True)


ax1.set_zorder(1)
ax1.patch.set_alpha(0)
ax2.set_zorder(0)

ax1.yaxis.tick_right()
ax2.yaxis.tick_left()

plt.show()

enter image description here