2015-10-24 35 views
0

我想知道当我有2个y轴时,如何使用matplotlib来制作一个平方图。这里有一个例子:如何在matplotlib中使用2个不同的y轴时做平方图?

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

正如你所看到的,所产生的情节是不是方形: enter image description here

到目前为止,我已经尝试了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())
  • scaling在ax2中使用的数据值,所以他们调整幅度的ax1
  • fig.set_size_inches(fig.get_size_inches()[0], fig.get_size_inches()[0])迫使图像平方,但我用尺子测量了x和y轴,它们的大小是不同的(略有差异)

我使用的数据有2个不同尺度:所述第一y轴的范围从0到250,而第二一个范围从0到100(这就是为什么我考虑通过在AX2使用的所有值乘以2.5倍)。我确信有一些明显的我没有看到,所以提前谢谢你。

回答

1

您是否希望您的坐标轴长度相等,或者您希望坐标轴上的缩放比例是否相等,这并不完全清楚。

为了得到正方形的长宽比,我创建了一个正方形尺寸为fig = plt.figure(figsize=(5,5))的图形,这足以获得长度相同的轴。

enter image description here

要获得相同的缩放所有轴,我添加了set_scaling()说明

enter image description here

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') 
相关问题