Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use matplotlib log scale in a graphic with two y axes?

# Criar figura e eixos
fig, ax1 = plt.subplots()



# Configurar o primeiro eixo (temperatura) e todas as curvas em função desse eixo
ax1.set_xlabel('Tempo (s)')
ax1.set_ylabel('Temperaturas (ºC)')

ax1.plot(df["Tempo"], df["Temp. Forno"], color='tab:red', label='Forno')
ax1.plot(df["Tempo"], df["ViewPort"], color='tab:orange', label='ViewPort')
ax1.plot(df["Tempo"], df["Água Flange"], color='tab:olive', label='Água Flange')
ax1.plot(df["Tempo"], df["Flange Inox"], color='tab:purple', label='Flange Inox')
ax1.plot(df["Tempo"], df["Tampa"], color='tab:pink', label='Tampa')

ax1.tick_params(axis='y')



# Criar o segundo eixo (pressão)
ax2 = ax1.twinx()
ax2.set_ylabel('Pressão (mBar)')
ax2.plot(df["Tempo"], df["PressaoAlta"], color='tab:blue')
ax2.tick_params(axis='y')

#Abaixo segue uma tentativa de tornar o segundo eixo uma escala logarítmica
ax2.set_yscale('log')
ax2.yaxis.set_major_locator(ticker.LogLocator(subs=(1.0, 2.5, 5.0)))
ax2.yaxis.set_major_formatter(ticker.ScalarFormatter())



# Adicionar legenda
ax1.legend(bbox_to_anchor=(1.2, 1.1), loc='upper left')
ax2.legend(['Pressão'], bbox_to_anchor=(1.435, 0.75), loc='upper right')



# Exibir o gráfico
plt.savefig('grafico')
plt.show()

My graphic

As we can see in the graphic, the right column is 0.0001, for example, and I want this and the subs in base equal to 10, like 10^-4.
I want the code to show the ax2 in scientific notation in log scale on the graphic. What can I do?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As per Set ‘y’ axis to scientific notation, you can set an axis to scientific notation using ax2.ticklabel_format(axis="y", style="sci"). In the answer, they used "both", which sets both the x and y axes to scientific notation, but you only need to apply it to the y-axis.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading