简体   繁体   English

在matplotlib中使用2个不同的y轴时,如何制作平方图?

[英]How to make a squared plot when using 2 different y-axis in matplotlib?

I would like to know how can I make a squared plot using matplotlib when I have 2 y-axis. 我想知道当我有2个y轴时如何使用matplotlib绘制平方图。 Here is an example: 这是一个例子:

    import matplotlib.pyplot as plt
    import seaborn as sns

    gammas = sns.load_dataset("gammas")
    sns.set(context="paper", palette="colorblind", style="ticks")
    fig, ax1 = plot.subplots()
    sns.tsplot(gammas[(gammas["ROI"] == "IPS")].reset_index(), time="timepoint", unit="subject", value="BOLD signal", ci=95, color="#4477AA", legend=False, ax=ax1)
    ax1.set_xlabel("Timepoint")
    ax1.set_ylabel("BOLD signal (1)")
    ax1.spines["top"].set_visible(False)
    ax1.tick_params(top='off')
    ax2 = ax1.twinx()
    ax2.yaxis.tick_right()
    ax2.yaxis.set_label_position("right")
    sns.tsplot(gammas[(gammas["ROI"] == "AG")].reset_index(), time="timepoint", unit="subject", value="BOLD signal", ci=95, color="#CC6677", legend=False, ax=ax2)
    ax2.set_ylabel("BOLD signal (2)")
    ax2.spines["top"].set_visible(False)
    ax2.tick_params(top='off')
    # Set legend #
    ax2.legend([ax1.get_lines()[0], ax2.get_lines()[0]], ["IPS", "AG"], loc='upper left')
    plt.show()

As you can see, the resulting plot is not squared: 如您所见,结果图不是平方的: 在此处输入图片说明

So far, I have tried the following before the plt.show() command: 到目前为止,我已经在plt.show()命令之前尝试了以下方法:

  • ax1.set_aspect(1. / ax1.get_data_ratio())
  • ax1.set_aspect(1. / ax1.get_data_ratio()) and ax2.set_aspect(1. / ax2.get_data_ratio()) ax1.set_aspect(1. / ax1.get_data_ratio())ax2.set_aspect(1. / ax2.get_data_ratio())
  • scaling the data values used in ax2 so they adjust in magnitude to the values in ax1 缩放ax2中使用的数据值,以便将其大小调整为ax1中的值
  • fig.set_size_inches(fig.get_size_inches()[0], fig.get_size_inches()[0]) to force the image to be squared, but I have measured the x and y axis with a ruler and their size is different (by a slight difference) fig.set_size_inches(fig.get_size_inches()[0], fig.get_size_inches()[0])强制将图像平方,但是我用标尺测量了x和y轴,并且它们的大小不同(略有差异)

The data I am using has 2 different scales: the 1st y-axis ranges from 0 to 250 while the 2nd one ranges from 0 to 100 (this is why I thought about multiplying all values used in ax2 by a factor of 2.5). 我正在使用的数据具有2个不同的标度:第一个y轴的范围是0到250,而第二个y轴的范围是0到100(这就是为什么我考虑将ax2中使用的所有值乘以2.5的原因)。 I am sure there is something obvious that I am not seeing, so thank you in advance. 我敢肯定我看不到明显的东西,所以请先谢谢。

It's not entirely clear whether you want your axes to be of equal length, or whether you want the scaling on your axes to be equal. 是否要使轴长度相等还是要使轴的缩放比例相等尚不完全清楚。

To get a square aspect ratio, I created a figure with a square dimension fig = plt.figure(figsize=(5,5)) this is enough to get axes that are the same length. 为了获得长宽比,我创建了一个具有正方形尺寸的图形fig = plt.figure(figsize=(5,5))这足以获得相同长度的轴。

在此处输入图片说明

To get the same scaling on all axes, I added the set_scaling() instructions 为了在所有轴上获得相同的缩放比例,我添加了set_scaling()指令

在此处输入图片说明

import matplotlib.pyplot as plt
import seaborn as sns

gammas = sns.load_dataset("gammas")
sns.set(context="paper", palette="colorblind", style="ticks")
fig = plt.figure(figsize=(5,5))
ax1 = fig.add_subplot(111)
sns.tsplot(gammas[(gammas["ROI"] == "IPS")].reset_index(), time="timepoint", unit="subject", value="BOLD signal", ci=95, color="#4477AA", legend=False, ax=ax1)
ax1.set_xlabel("Timepoint")
ax1.set_ylabel("BOLD signal (1)")
ax1.spines["top"].set_visible(False)
ax1.tick_params(top='off')
ax2 = ax1.twinx()
ax2.yaxis.tick_right()
ax2.yaxis.set_label_position("right")
sns.tsplot(gammas[(gammas["ROI"] == "AG")].reset_index(), time="timepoint", unit="subject", value="BOLD signal", ci=95, color="#CC6677", legend=False, ax=ax2)
ax2.set_ylabel("BOLD signal (2)")
ax2.spines["top"].set_visible(False)
ax2.tick_params(top='off')
# Set legend #
ax2.legend([ax1.get_lines()[0], ax2.get_lines()[0]], ["IPS", "AG"], loc='upper left')
# set the aspect ratio so that the scaling is the same on all the axes
ax1.set_aspect('equal')
ax2.set_aspect('equal')

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

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