如何在具有两个y轴的图形中使用matplotlib的对数刻度?

huangapple go评论90阅读模式
英文:

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

问题

  1. # Criar figura e eixos
  2. fig, ax1 = plt.subplots()
  3. # Configurar o primeiro eixo (temperatura) e todas as curvas em função desse eixo
  4. ax1.set_xlabel('Tempo (s)')
  5. ax1.set_ylabel('Temperaturas (°C)')
  6. ax1.plot(df["Tempo"], df["Temp. Forno"], color='tab:red', label='Forno')
  7. ax1.plot(df["Tempo"], df["ViewPort"], color='tab:orange', label='ViewPort')
  8. ax1.plot(df["Tempo"], df["Água Flange"], color='tab:olive', label='Água Flange')
  9. ax1.plot(df["Tempo"], df["Flange Inox"], color='tab:purple', label='Flange Inox')
  10. ax1.plot(df["Tempo"], df["Tampa"], color='tab:pink', label='Tampa')
  11. ax1.tick_params(axis='y')
  12. # Criar o segundo eixo (pressão)
  13. ax2 = ax1.twinx()
  14. ax2.set_ylabel('Pressão (mBar)')
  15. ax2.plot(df["Tempo"], df["PressaoAlta"], color='tab:blue')
  16. ax2.tick_params(axis='y')
  17. # Abaixo segue uma tentativa de tornar o segundo eixo uma escala logarítmica
  18. ax2.set_yscale('log')
  19. ax2.yaxis.set_major_locator(ticker.LogLocator(base=10.0))
  20. ax2.yaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True, useOffset=False))
  21. # Adicionar legenda
  22. ax1.legend(bbox_to_anchor=(1.2, 1.1), loc='upper left')
  23. ax2.legend(['Pressão'], bbox_to_anchor=(1.435, 0.75), loc='upper right')
  24. # Exibir o gráfico
  25. plt.savefig('grafico')
  26. plt.show()
英文:
  1. # Criar figura e eixos
  2. fig, ax1 = plt.subplots()
  3. # Configurar o primeiro eixo (temperatura) e todas as curvas em função desse eixo
  4. ax1.set_xlabel('Tempo (s)')
  5. ax1.set_ylabel('Temperaturas (ºC)')
  6. ax1.plot(df["Tempo"], df["Temp. Forno"], color='tab:red', label='Forno')
  7. ax1.plot(df["Tempo"], df["ViewPort"], color='tab:orange', label='ViewPort')
  8. ax1.plot(df["Tempo"], df["Água Flange"], color='tab:olive', label='Água Flange')
  9. ax1.plot(df["Tempo"], df["Flange Inox"], color='tab:purple', label='Flange Inox')
  10. ax1.plot(df["Tempo"], df["Tampa"], color='tab:pink', label='Tampa')
  11. ax1.tick_params(axis='y')
  12. # Criar o segundo eixo (pressão)
  13. ax2 = ax1.twinx()
  14. ax2.set_ylabel('Pressão (mBar)')
  15. ax2.plot(df["Tempo"], df["PressaoAlta"], color='tab:blue')
  16. ax2.tick_params(axis='y')
  17. #Abaixo segue uma tentativa de tornar o segundo eixo uma escala logarítmica
  18. ax2.set_yscale('log')
  19. ax2.yaxis.set_major_locator(ticker.LogLocator(subs=(1.0, 2.5, 5.0)))
  20. ax2.yaxis.set_major_formatter(ticker.ScalarFormatter())
  21. # Adicionar legenda
  22. ax1.legend(bbox_to_anchor=(1.2, 1.1), loc='upper left')
  23. ax2.legend(['Pressão'], bbox_to_anchor=(1.435, 0.75), loc='upper right')
  24. # Exibir o gráfico
  25. plt.savefig('grafico')
  26. plt.show()

如何在具有两个y轴的图形中使用matplotlib的对数刻度?

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?

答案1

得分: 0

根据将'y'轴设置为科学计数法,您可以使用ax2.ticklabel_format(axis="y", style="sci")将轴设置为科学计数法。在答案中,他们使用了"both",这会将x轴和y轴都设置为科学计数法,但您只需要将其应用于y轴。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月13日 00:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定