1

I have a dataframe,df, which i intend to plot a line chart where we cut the window in several subplots, one per group. Then,each group contains each column which is represented by a thick line displayed on its unique subplot while the other columns are displayed discreetly like the picture below so it won't lead to a spaghetti plot. The dimension of the dataframe is (30,26)

Index        DateTimestamp              0.0      5.0     34.0 ... 22.0 
  0        2017-08-03 00:00:00           10        0      10       0
  1        2017-08-04 00:00:00           20       60    1470      20
  3        2017-08-05 00:00:00           0        58       0      24
  4        2017-08-06 00:00:00           0         0     480      24
  5        2017-09-07 00:00:00           0         0       0      25
  :                 :                     :         :       :      :
  :                 :                     :         :       :      :
 29       2017-09-30 00:00:00

It should look this:

enter image description here

the y4, y5 .... ontop of each subplot in the picture is the particular column name being displayed which in my dataframe can be 0.0,1.0....25.0. The x-axis tick labels will be the datetimestamp column of my dataframe,df. the Y-axis is the range of values of for all columns. Here is the code: #Initialize the figure

plt.style.use('seaborn-darkgrid')
#create a color palette
palette = plt.get_cmap('tab20')
#multiple line plot
num=0
for column in df.drop(' DateTimestamp', axis=1):
num+=1
# Find the right spot on the plot
plt.subplot(6,5, num)
# plot every groups, but discreet
for v in df.drop(' DateTimestamp', axis=1):
    plt.plot(df[' DateTimestamp'], df[v], marker='', color='grey', linewidth=0.6, alpha=0.3)
# Plot the lineplot
plt.plot(df[' DateTimestamp'], df[column], marker='', color=palette(num), linewidth=2.4, alpha=0.9, label=column)
# Same limits for everybody!
plt.xlim(0,30)
plt.ylim(0,0) # the limit of the y-axis should be the max value of all possible column values 
# Not ticks everywhere
if num in range(7) :
    plt.tick_params(labelbottom='off')
if num not in [1,4,7] :
    plt.tick_params(labelleft='off')
# Add title
 plt.title(column, loc='left', fontsize=12, fontweight=0, color=palette(num) )
#general title
 plt.suptitle("Line Plot",fontsize=13, fontweight=0, color='black', style='italic', y=1.02)
#Axis title
plt.text(0.5, 0.02, 'Time', ha='center', va='center')
plt.text(0.06, 0.5, 'Note', ha='center', va='center', rotation='vertical')

However,i get error:

Traceback (most recent call last):
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 307, in __call__return printer(obj)
 File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 240, in <lambda> png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
 File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 124, in print_figure fig.canvas.print_figure(bytes_io, **kw)
 File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2212, in print_figure**kwargs)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 513, in print_png FigureCanvasAgg.draw(self)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw self.figure.draw(self.renderer)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1475, in draw renderer, self, artists, self.suppressComposite)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images a.draw(renderer)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images a.draw(renderer)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1190, in draw ticks_to_draw = self._update_ticks(renderer)
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1028, in _update_ticks tick_tups = list(self.iter_ticks())  # iter_ticks calls the locator
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\axis.py", line 971, in iter_ticks majorLocs = self.major.locator()
  File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1249, in __call__self.refresh()
 File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1269, in refresh dmin, dmax = self.viewlim_to_dt()
 File "C:\Users\ty\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1026, in viewlim_to_dt.format(vmin))
ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units <Figure size 432x288 with 25 Axes>
Bode
  • 527
  • 2
  • 9
  • 19
  • What purpose does `plt.xlim(0,30)` serve? I suppose 0 and 30 are way off the real data and 0 is not a valid date, hence the error. – ImportanceOfBeingErnest Nov 11 '18 at 02:09
  • Thanks for your response.I was thinking to represent xlim(0,30) as the number of days. What do you propose as the likely solution. – Bode Nov 11 '18 at 09:23
  • If you have a column in your dataframe which contains the numbers 0 to 30 you can use `xlim(0,30)`. But currently you are plotting the dates on the x axis. If your dates are sorted and equally spaced, you might just plot the dataframe index instead of the dates, `plt.plot(df.index, df[column])`. – ImportanceOfBeingErnest Nov 11 '18 at 11:59
  • Thanks a lot ...but there are some days that are missing so the dates are not equally spaced...so the plot won't be accurate – Bode Nov 11 '18 at 14:26
  • My comment above contains two options. I suppose you are then looking for the first one? – ImportanceOfBeingErnest Nov 11 '18 at 14:33
  • I dont have a column that has 0 to 30 in the dataframe. I intend to plot the datetimestamp column to be the x-axis ranging from the 1st day to the last day of the month in the column – Bode Nov 11 '18 at 15:18
  • Sorry for not expressing that clearly. You can create a new column in your dataframe which contains the day-number you want to plot. Similar to [this](https://stackoverflow.com/questions/22923775/calculate-pandas-dataframe-time-difference-between-two-columns-in-hours-and-minu). – ImportanceOfBeingErnest Nov 11 '18 at 15:32
  • Thanks a lot. It worked but the figure is blurry and the x-axis is clashing between the subplots of the previous ones. Any idea what i can do to increase the resolution for each of the subplot and spacing between the previous subplots and the current one – Bode Nov 12 '18 at 12:50
  • Seems like a different problem than the one this question asks about. – ImportanceOfBeingErnest Nov 12 '18 at 13:59

0 Answers0