Home > Back-end >  Change axis from hours to months in matplotlib
Change axis from hours to months in matplotlib

Time:07-06

I'm a beginning programmer, and I'm working on plotting data in matplotlib. The plot is supposed to show nitrogen content over the course of hours. However, as the model runs over several months, it would be better to show months instead of hours on the x-axis.

enter image description here

CodePudding user response:

Apparently, you can get an approximation of the number of months by dividing the number of hours by 730:

hours-to-months-formula

import matplotlib.pyplot as plt


def hours_to_months_approximation(hours: int) -> float:
    return round(hours / 730, 2)


def main() -> None:
    plt.figure()
    plt.title('Nitrogen content')
    plt.plot(sugar_kelp_field.N_content_list)
    plt.plot(hours_to_months_approximation(720), 0.0150, 'bo')
    plt.plot(hours_to_months_approximation(1440), 0.0205, 'bo')
    plt.plot(hours_to_months_approximation(2160), 0.0265, 'bo')
    plt.plot(hours_to_months_approximation(2880), 0.0283, 'bo')
    plt.plot(hours_to_months_approximation(3600), 0.0234, 'bo')
    plt.plot(hours_to_months_approximation(4320), 0.0181, 'bo')
    plt.plot(hours_to_months_approximation(5040), 0.0142, 'bo')
    plt.plot(hours_to_months_approximation(5760), 0.0097, 'bo')
    plt.plot(hours_to_months_approximation(6480), 0.0083, 'bo')
    plt.xlabel('time [months]')
    plt.ylabel('nitrogen content [fraction of dw]')
    plt.show()


if __name__ == '__main__':
    main()

  • Related