如何在gridspec的图的另一侧显示轴刻度标签?

时间:2016-08-05 01:53:22

标签: python matplotlib

绘制我最喜欢的示例数据框,如下所示:

      x   val1   val2   val3
0    0.0  10.0   NaN    NaN
1    0.5  10.5   NaN    NaN
2    1.0  11.0   NaN    NaN
3    1.5  11.5   NaN  11.60
4    2.0  12.0   NaN  12.08
5    2.5  12.5  12.2  12.56
6    3.0  13.0  19.8  13.04
7    3.5  13.5  13.3  13.52
8    4.0  14.0  19.8  14.00
9    4.5  14.5  14.4  14.48
10   5.0   NaN  19.8  14.96
11   5.5  15.5  15.5  15.44
12   6.0  16.0  19.8  15.92
13   6.5  16.5  16.6  16.40
14   7.0  17.0  19.8  18.00
15   7.5  17.5  17.7    NaN
16   8.0  18.0  19.8    NaN
17   8.5  18.5  18.8    NaN
18   9.0  19.0  19.8    NaN
19   9.5  19.5  19.9    NaN
20  10.0  20.0  19.8    NaN

我有两个子图,由于其他原因,我最好使用gridspec。绘图代码如下(它非常全面,所以我希望避免代码中的重大更改,否则它们可以完美地工作,并且不会做一个不重要的细节):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
import matplotlib as mpl

df = pd.read_csv('H:/DocumentsRedir/pokus/dataframe.csv', delimiter=',')

# setting limits for x and y
ylimit=(0,10)
yticks1=np.arange(0,11,1)
xlimit1=(10,20)
xticks1 = np.arange(10,21,1)

# general plot formatting (axes colour, background etc.)
plt.style.use('ggplot')
plt.rc('axes',edgecolor='black')
plt.rc('axes', facecolor = 'white')
plt.rc('grid', color = 'grey')
plt.rc('grid', alpha = 0.3) # alpha is percentage of transparency
colours = ['g','b','r']
title1 = 'The plot'

# GRIDSPEC INTRO - rows, cols, distance of individual plots
fig = plt.figure(figsize=(6,4))
gs=gridspec.GridSpec(1,2, hspace=0.15, wspace=0.08,width_ratios=[1,1])

## SUBPLOT of GRIDSPEC with lines 
# the first plot
axes1 = plt.subplot(gs[0,0])  

for count, vals in enumerate(df.columns.values[1:]):

    X = np.asarray(df[vals])

    h = vals
    p1 = plt.plot(X,df.index,color=colours[count],linestyle='-',linewidth=1.5,label=h) 

# formatting
p1 = plt.ylim(ylimit) 
p1 = plt.yticks(yticks1, yticks1, rotation=0) 
p1 = axes1.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.1))
p1 = plt.setp(axes1.get_yticklabels(),fontsize=8)
p1 = plt.gca().invert_yaxis()
p1 = plt.ylabel('x [unit]', fontsize=14)
p1 = plt.xlabel("Value [unit]", fontsize=14)
p1 = plt.tick_params('both', length=5, width=1, which='minor', direction = 'in')
p1 = axes1.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.1))
p1 = plt.xlim(xlimit1)
p1 = plt.xticks(xticks1, xticks1, rotation=0)
p1 = plt.setp(axes1.get_xticklabels(),fontsize=8)
p1 = plt.legend(loc='best',fontsize = 8, ncol=2)   #

# the second plot (something random)
axes2 = plt.subplot(gs[0,1]) 

for count, vals in enumerate(df.columns.values[1:]):

   nonans = df[vals].dropna()
   result=nonans-0.5

   p2 = plt.plot(result,nonans.index,color=colours[count],linestyle='-',linewidth=1.5)

p2 = plt.ylim(ylimit)
p2 = plt.yticks(yticks1, yticks1, rotation=0)
p2 = axes2.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.1))
p2 = plt.gca().invert_yaxis()
p2 = plt.xlim(xlimit1)
p2 = plt.xticks(xticks1, xticks1, rotation=0)
p2 = axes2.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.1))
p2 = plt.setp(axes2.get_xticklabels(),fontsize=8)
p2 = plt.xlabel("Other value [unit]", fontsize=14)
p2 = plt.tick_params('x', length=5, width=1, which='minor', direction = 'in')
p2 = plt.setp(axes2.get_yticklabels(), visible=False)

fig.suptitle(title1, size=16)
plt.show()

但是,是否可以在右侧显示第二个子图的y刻度标签?当前代码产生了这个:

enter image description here

我想知道是否有一种简单的方法可以解决这个问题: enter image description here

2 个答案:

答案 0 :(得分:2)

尝试类似

的内容
axes2.yaxis.tick_right()

只需环顾Python Matplotlib Y-Axis ticks on Right Side of Plot

答案 1 :(得分:0)

不,好的,发现它正是我想要的。 我希望TICKS在两侧,只是标签在右侧。上面的解决方案从子图的左侧删除了我的刻度,这看起来并不好看。但是,这answer似乎得到了正确的解决方案:) 总结一下: 要获得两侧的刻度和右侧的标签,这就是修复它的原因:

axes2.yaxis.tick_right(‌​) 
axes2.yaxis.set_ticks_p‌​osition('both')

如果x轴需要相同,那么它是axes2.xaxis.tick_top(‌​)