I need to create a plot using matplotlib.pyplot
that shows distance between Earth and Mars over time. In addition to that some months e.g. March to August should be shown in a different color than the other months.
Data is provided from an array containing the date, distance and also a flag indicating whether the date is in the March to August-span or not.
The array containing the whole data is called master_array
. First column containing the date, second the distance; seventh the s/w-flag. ('s' for summer, 'w' for winter).
I tried to make use of the fact that pyplot.plot
switches color for every single .plot
command in a way that I first plot the winter months and in a second plot the summer months.
import numpy as np
from matplotlib import pyplot as plt
def md_plot2(dt64=np.array, md=np.array):
"""Erzeugt Plot der Marsdistanz (y-Achse) zur Zeit (x-Achse)."""
plt.style.use('seaborn-whitegrid')
y, m, d = dt64.astype(int) // np.c_[[10000, 100, 1]] % np.c_[[10000, 100, 100]]
dt64 = y.astype('U4').astype('M8') + (m-1).astype('m8[M]') + (d-1).astype('m8[D]')
wFilter = np.argwhere(master_array[:,6] == 0)
sFilter = np.argwhere(master_array[:,6] == 1)
plt.plot(dt64[wFilter], md[wFilter], label='Halbjahr der fallenden \nTemperaturen')
plt.plot(dt64[sFilter], md[sFilter], label='Halbjahr der steigenden \nTemperaturen')
plt.xlabel("Zeit in Jahren\n")
plt.xticks(rotation = 45)
plt.ylabel("Marsdistanz in AE\n(1 AE = 149.597.870,7 km)")
plt.legend(loc='upper right', frameon=True)
plt.figure('global betrachtet...') # diesen Block ggf. auskommentieren
#plt.style.use('seaborn-whitegrid')
md_plot2(master_array[:,0], master_array[:,1]) # Graph
plt.show()
#plt.close()
Problem now is that between the last point of a summer period and first point of the following summer period the plot for summers shows a line where the other plot (for winters) shows the correct data of the winter period that lays between those two summers.
The data is containing many data points what will lead to many segments of those two colors.
How can I stop the plot to draw a line, when the next data point is more that one day in the future?
Is there maybe another method of pyplot
for this task?
As I wrote a lot of code in the script where this belongs without using pandas I would be very happy to find a solution to this problem not using pandas as I try to avoid it because I don't want to bloat my script when there is a way to get the task done by using the modules I am already using. I also read it would decrease the speed here.