簡體   English   中英

旋轉x軸文本並將y標簽移動到軸的頂部(多個y軸)

[英]rotate x-axis text and move y-labels to the top of axes (multiple y-axes)

在下面的python代碼中,我正在繪制數據幀中的時間數據和多個y值。 現在我想: - 垂直旋轉x軸的時間值 - 將所有y標簽(y1-y4)移動到軸的頂部是否有人有建議或解決方案?

import pandas as pd
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

data = {'time':['00:00:00','18:00:00','23:59:00'],
          'y1': [1,2,3],'y2': [4,5,6],'y3': [7,8,9],'y4': [10,11,12]}

df=pd.DataFrame(data,columns=['time','y1','y2','y3','y4'])
df['time']=pd.to_datetime(df['time'],format='%H:%M:%S')

host=host_subplot(111,axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)
par1=host.twinx()
par2=host.twinx()
par3=host.twinx()
offset1=40
offset2=80 
new_fixed_axis=par2.get_grid_helper().new_fixed_axis

par2.axis['right']=new_fixed_axis(loc='right',axes=par2,offset=(offset1,0))
par2.axis['right'].toggle(all=True)
par3.axis['right']=new_fixed_axis(loc='right',axes=par3,offset=(offset2,0))
par3.axis['right'].toggle(all=True)

host.set_xlabel('Time')
host.set_ylabel('y1')
par1.set_ylabel('y2')
par2.set_ylabel('y3')
par3.set_ylabel('y4')

p1,=host.plot(df['time'],df['y1'])
p2,=par1.plot(df['time'],df['y2'])
p3,=par2.plot(df['time'],df['y3'])
p4,=par3.plot(df['time'],df['y4'])

host.set_ylim(0,5)
par1.set_ylim(0,8)
par2.set_ylim(0,10)
par3.set_ylim(0,15)

host.legend(loc='upper left',bbox_to_anchor=(0,-.15),ncol=4) 
host.axis['left'].label.set_color(p1.get_color())
host.axis["left"].label.set_rotation(90)

par1.axis['right'].label.set_color(p2.get_color())
par1.axis["right"].label.set_rotation(-90)

par2.axis['right'].label.set_color(p3.get_color())
par2.axis["right"].label.set_rotation(-90)

par3.axis['right'].label.set_color(p4.get_color())
par3.axis["right"].label.set_rotation(-90)

plt.draw()
plt.show()

在此輸入圖像描述

我有第一個問題的解決方案,第二個問題是一個丑陋的黑客。

1)旋轉x軸文本

您需要使用host.axis屬性設置它,然后向下移動圖例和xlabel以騰出空間:

host.legend(loc='upper left',bbox_to_anchor=(0,-.3),ncol=4)  # Lower legend a bit
host.axis["bottom"].label.set_pad(50)  # Lower x-label a bit
host.axis["bottom"].major_ticklabels.set(  # This is the actual rotation command
    rotation=90, verticalalignment='center', horizontalalignment='right', pad=-2)

2)將y標簽移動到軸的頂部

我在這里嘗試的沒有任何工作。 具體來說,我希望以下工作:

par1.axis["right"].label.set_position((1.0, 1.0))

但是相應的get_position命令保持返回(1.0, 0.5) get_position (1.0, 0.5) ,其中0.5是軸的中間。 由於某種原因,'set'命令根本不會堅持。 這是丑陋的黑客入侵的地方 - 在標簽下方添加空白行 因此,在設置標簽文本的位置,請更改為:

new_line_pos_fix = ''.join(['\n'] * 9)  # 9 is the number of blank lines; change as needed
host.set_ylabel('y1' + new_line_pos_fix)
par1.set_ylabel('y2' + new_line_pos_fix)
par2.set_ylabel('y3' + new_line_pos_fix)
par3.set_ylabel('y4' + new_line_pos_fix)

現在我們只需要設置標簽的垂直對齊方式,使空行生效,添加一些填充以提高可讀性:

host.axis['right'].label.set(verticalalignment='bottom', pad=7)
par1.axis['right'].label.set(verticalalignment='bottom', pad=7)
par2.axis['right'].label.set(verticalalignment='bottom', pad=7)
par3.axis['right'].label.set(verticalalignment='bottom', pad=7)

如果您想要更有條理的代碼,上述命令可以與顏色和旋轉設置結合使用,例如:

par1.axis['right'].label.set(color=p2.get_color(), rotation=-90, verticalalignment='bottom', pad=7)

結果:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM