简体   繁体   English

将负y轴转换为正(matplotlib)

[英]Convert negative y axis to positive (matplotlib)

I want to plot bar chart for some parameters for men and women. 我想绘制条形图,为男性和女性绘制一些参数。 I have done like this: 我这样做了: 在此输入图像描述

I want to show the frequency for mean in upper side (positive y axis) and for women in lower side (negative x-axis). 我想显示上侧(正y轴)和下侧(负x轴)的女性的平均频率。 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,...) . 我可以在负y轴上更改标记(-5,-10,...这里),使它们的大小保持相同但都是正数(5,10,...)。 Now there should be two positive y-axis , one mirror image of other. 现在应该有两个正y轴,一个是其他的镜像。

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()

Here I just set the yticklabel to be the absolute value of the y position. 在这里,我只是将yticklabel设置为y位置的绝对值。 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 如果你想让它在中间“镜像”,你需要用ax.set_ylim设置一个明确的y范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM