将负 y 轴转换为正 (matplotlib) [英] Convert negative y axis to positive (matplotlib)

查看:54
本文介绍了将负 y 轴转换为正 (matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一些男性和女性参数的条形图.我这样做了:

I want to plot bar chart for some parameters for men and women. I have done like this:

我想显示上侧(正 y 轴)和下侧(负 x 轴)女性的平均频率.在这种情况下,对于频率,幅度大小和符号无关紧要.为方便起见,我在上侧显示一个,在下侧显示另一个.我可以在负y轴上更改标签(此处为-5,-10,...),以使其大小保持不变,但均为正(5、10,...).现在应该有两个正 y 轴,一个是另一个的镜像.

I want to show the frequency for mean in upper side (positive y axis) and for women in lower side (negative x-axis). In this case, for frequency only the magnitude matter and sign does not. Just for convenience I am showing one in upper side and another in lower side. Can I change the labeling (-5, -10, ... here) in negative y-axis so that their magnitude remain same but all are positive (5, 10,...) . Now there should be two positive y-axis , one mirror image of other.

推荐答案

当然可以.下面是一个您可以尝试的示例:

Sure it can be done. Here's an example that you can play around with:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax.plot(x,y)
ax.set_yticklabels([str(abs(x)) for x in ax.get_yticks()])
ax.show()

这里我只是将 yticklabel 设置为 y 位置的绝对值.当然,我有对称数据,而你没有.如果要使其在中间镜像",则需要使用 ax.set_ylim

Here I just set the yticklabel to be the absolute value of the y position. Of course, I have symmetric data and you don't. If you want it to be "mirrored" down the middle, you'll need to set an explicit y range with ax.set_ylim

这篇关于将负 y 轴转换为正 (matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆