简体   繁体   中英

Matplotlib align ticks of secondary y axis to ticks of primary y axis

I need to align the ticks of two y axis. Unfortunately the solutions I've read do not work for me or, what I think is more probable, I just don't get it.

The Question:

I want to align the ticks of the secondary y axis to the ticks of the primary y axis. The plot shows power consumption with and without photovoltaik. The left ticks [0,50,100,150] should be aligned with the right ticks [-30,-20,-10,0] .在此处输入图像描述

Here's my code.

fig,ax1 = plt.subplots()
ax2 = ax1.twinx() 
lg1 = ax1.plot(df_P_and_PVA_av['time'], df_P_and_PVA_av['ges mean power'], label="Leistungsbezug Hauptzähler",color="blue", linewidth=0.9)
lg2 = ax1.plot(df_P_and_PVA_av['time'], df_P_and_PVA_av['combined mean power'], label="Leistungsbezug Hauptzähler ohne PVAs",color="blue", linewidth=0.9, linestyle='dotted')
lg3 = ax2.plot(df_P_and_PVA_av['time'], df_P_and_PVA_av['PVA mean power'], label="Leistung PVAs",color="lightblue", linewidth=0.9)

ax1.set_title("durchschnittlicher Lastgang Hauptzähler und PVA " + season_str + "\n" + str(df_P_period['date and time'].min().strftime('%d.%m.%Y')) + " bis " + str(df_P_period['date and time'].max().strftime('%d.%m.%Y')))
ax1.set_xlabel("Zeit")
ax1.set_ylabel("Leistung Hauptzähler [kW]")
ax2.set_ylabel("Leistung PVAs [kW]")


Anzahl_x_ticks = 6
x_ticks = list()
laenge = len(df_P_and_PVA_av['time'])
step = int(round(len(df_P_and_PVA_av['time'])/Anzahl_x_ticks))
for x in np.arange(Anzahl_x_ticks+1):
    if x == Anzahl_x_ticks:
        x_ticks.append(df_P_and_PVA_av['time'].max())
    else:
        x_ticks.append(df_P_and_PVA_av['time'].iloc[x*step])

ax1.set_xticks(x_ticks)
ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M'))


ax1.yaxis.set_major_locator(ticker.AutoLocator())
ax1.yaxis.set_minor_locator(ticker.AutoMinorLocator())
ax2.yaxis.set_major_locator(ticker.AutoLocator())
ax2.yaxis.set_minor_locator(ticker.AutoMinorLocator())


ratio = 1/3
x_left, x_right = ax1.get_xlim()
y_low, y_high = ax1.get_ylim()
ax1.set_aspect(abs((x_right-x_left)/(y_low-y_high))*ratio)


lg = lg1 + lg2 + lg3
labs = [l.get_label() for l in lg]


# Shrink current axis's height by 10% on the bottom
box = ax1.get_position()
ax1.set_position([box.x0, box.y0 + box.height * 0.1,
                  box.width, box.height * 0.9])

# Put a legend below current axis
ax1.legend(lg,labs,loc='upper center', bbox_to_anchor=(0.5, -0.3),
          fancybox=True, shadow=False, ncol=2, fontsize=10)


ax1.grid()
ax2.grid()

fig.tight_layout()

See data below:

time,PVA mean power,ges mean power,combined mean power
00:00,0.0,93.83310533515731,93.83310533515731
00:15,0.0,94.90560875512996,94.90560875512996
00:30,0.0,88.93023255813954,88.93023255813954
00:45,0.0,84.41586867305061,84.41586867305061
01:00,0.0,85.4938440492476,85.4938440492476
01:15,0.0,78.22708618331053,78.22708618331053
01:30,0.0,73.4829001367989,73.4829001367989
01:45,0.0,72.54172366621067,72.54172366621067
02:00,0.0,69.75923392612859,69.75923392612859
02:15,0.0,62.451436388508895,62.451436388508895
02:30,0.0,60.55813953488372,60.55813953488372
02:45,0.0,62.28180574555404,62.28180574555404
03:00,0.0,56.35020519835842,56.35020519835842
03:15,0.0,50.834473324213405,50.834473324213405
03:30,0.0,47.945280437756495,47.945280437756495
03:45,0.0,42.08481532147743,42.08481532147743
04:00,0.0,37.25854993160055,37.25854993160055
04:15,0.0,37.778385772913815,37.778385772913815
04:30,0.0,29.428180574555405,29.428180574555405
04:45,0.0,26.632010943912448,26.632010943912448
05:00,0.0,27.655266757865938,27.655266757865938
05:15,0.0,23.036935704514363,23.036935704514363
05:30,0.0,20.749658002735977,20.749658002735977
05:45,0.0,24.388508891928865,24.388508891928865
06:00,0.0,28.393980848153216,28.393980848153216
06:15,0.0,25.231190150478795,25.231190150478795
06:30,0.0,24.722298221614228,24.722298221614228
06:45,0.0,31.151846785225718,31.151846785225718
07:00,-0.0006195652173913,28.372093023255815,28.372712588473206
07:15,-0.0373152173913043,39.96169630642955,39.999011523820855
07:30,-0.1500326086956522,26.796169630642954,26.946202239338607
07:45,-0.3798586956521739,27.392612859097127,27.772471554749302
08:00,-0.7411739130434781,28.54172366621067,29.28289757925415
08:15,-1.2384782608695648,41.65253077975376,42.891009040623324
08:30,-1.952,30.555403556771545,32.50740355677154
08:45,-2.885717391304348,34.66484268125855,37.550560072562895
09:00,-4.00596739130435,41.86046511627907,45.866432507583426
09:15,-5.352141304347827,50.84541723666211,56.19755854100993
09:30,-6.705413043478261,47.102599179206564,53.80801222268482
09:45,-8.542423913043475,50.70861833105335,59.25104224409682
10:00,-10.197836956521735,57.55950752393981,67.75734448046154
10:15,-12.020652173913042,74.31463748290014,86.33528965681319
10:30,-13.50114130434782,77.09439124487004,90.59553254921786
10:45,-15.37723913043479,82.15868673050616,97.53592586094095
11:00,-17.448141304347825,81.38987688098496,98.83801818533279
11:15,-19.2846304347826,86.88372093023256,106.16835136501516
11:30,-20.54,87.44186046511628,107.98186046511628
11:45,-22.89032608695652,86.03009575923393,108.92042184619045
12:00,-23.88431521739129,92.19151846785226,116.07583368524355
12:15,-24.400217391304345,87.8358413132695,112.23605870457385
12:30,-25.06295652173912,83.46374829001368,108.52670481175281
12:45,-24.271391304347823,86.4514363885089,110.72282769285673
13:00,-25.826423913043477,85.05608755129958,110.88251146434305
13:15,-26.286391304347816,81.54309165526676,107.82948295961458
13:30,-25.576065217391307,83.68809849521203,109.26416371260333
13:45,-26.939999999999998,87.26675786593707,114.20675786593706
14:00,-27.62970652173913,91.26949384404925,118.89920036578837
14:15,-26.774228260869567,84.46511627906976,111.23934453993932
14:30,-25.4070760869565,89.78932968536252,115.19640577231903
14:45,-26.066369565217396,87.33242134062928,113.39879090584668
15:00,-24.71608695652174,92.87004103967168,117.58612799619341
15:15,-23.22843478260869,102.30369357045144,125.53212835306013
15:30,-21.706978260869555,98.14500683994528,119.85198510081483
15:45,-20.49426086956522,94.45690834473324,114.95116921429846
16:00,-18.77570652173913,95.8796169630643,114.65532348480343
16:15,-16.73016304347826,102.25991792065663,118.99008096413489
16:30,-15.182967391304356,99.7045143638851,114.88748175518946
16:45,-13.26690217391305,99.37072503419972,112.63762720811278
17:00,-12.464771739130436,107.23392612859097,119.6986978677214
17:15,-10.54741304347826,102.9329685362517,113.48038157972996
17:30,-8.76653260869565,101.98084815321478,110.74738076191043
17:45,-7.517326086956521,108.1641586867305,115.68148477368702
18:00,-5.8470434782608685,109.25854993160054,115.10559340986141
18:15,-4.581456521739131,105.04514363885089,109.62660016059002
18:30,-3.26388043478261,109.34610123119015,112.60998166597275
18:45,-2.263402173913044,112.2626538987688,114.52605607268185
19:00,-1.6043586956521736,116.63474692202462,118.23910561767678
19:15,-0.977195652173913,115.74281805745554,116.72001370962946
19:30,-0.5131630434782608,116.93570451436389,117.44886755784215
19:45,-0.2263804347826086,113.84952120383036,114.07590163861298
20:00,-0.0580543478260869,116.13679890560876,116.19485325343484
20:15,-0.0065217391304347,118.69767441860465,118.70419615773508
20:30,-1.0869565217391303e-05,114.34746922024624,114.34748008981146
20:45,0.0,114.81805745554036,114.81805745554036
21:00,0.0,119.05882352941177,119.05882352941177
21:15,0.0,115.56771545827634,115.56771545827634
21:30,0.0,114.76333789329685,114.76333789329685
21:45,0.0,114.75786593707251,114.75786593707251
22:00,0.0,112.89192886456908,112.89192886456908
22:15,0.0,103.26128590971273,103.26128590971273
22:30,0.0,103.85225718194255,103.85225718194255
22:45,0.0,108.55813953488372,108.55813953488372
23:00,0.0,105.38987688098496,105.38987688098496
23:15,0.0,100.1641586867305,100.1641586867305
23:30,0.0,100.54172366621067,100.54172366621067
23:45,0.0,95.48016415868673,95.48016415868673
24:00,0.0,93.83310533515731,93.83310533515731

If you know in advance to which values you want to limit the two y axis, then you can use matplotlib.axes.Axes.set_ylim and matplotlib.axes.Axes.set_yticks :

n_ticks = 4
y1_min = 0
y1_max = 150
y2_min = -30
y2_max = 0

ax1.set_yticks(np.linspace(y1_min, y1_max, n_ticks))
ax2.set_yticks(np.linspace(y2_min, y2_max, n_ticks))
ax1.set_ylim(y1_min, y1_max)
ax2.set_ylim(y2_min, y2_max)

In order to make it works properly, you need to remove this line:

# ax1.set_aspect(abs((x_right-x_left)/(y_low-y_high))*ratio)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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