2

I would like to remove the flat lines on my graph by keeping the labels x.

I have this code which gives me a picture

dates = df_stock.loc[start_date:end_date].index.values
x_values = np.array([datetime.datetime.strptime(d, "%Y-%m-%d %H:%M:%S") for d in dates])

fig, ax = plt.subplots(figsize=(15,9))

# y values
y_values = np.array(df_stock.loc[start_date:end_date, 'Bid'])
# plotting
_ = ax.plot(x_values, y_values, label='Bid')

# formatting
formatter = mdates.DateFormatter('%m-%d %H:%M')
ax.xaxis.set_major_formatter(formatter)

enter image description here

The flat lines correspond to data which does not exist I would like to know if it is possible not to display them while keeping the gap of the x labels.

thank you so much

0

1 Answer 1

3

You want to have time on the x-axis and time is equidistant -- independent whether you have data or not. You now have several options:

  1. don't use time on the x-axis but samples/index
  2. do as in 1. but change the ticks & labels to draw time again (but this time not equidistantly)
  3. make the value-vector equidistant and use NaNs to fill the gaps

Why is this so? Per default, matplotlib produces a line plot, which connects the points with lines using the order in which they are presented. In contrast to this a scatter plot just plots the individual points, not suggesting any underlying order. You achieve the same result as if you would use a line plot without markers.

In general, you have 3-4 options

  1. use the plot command but only plot markers (add linestyle='')
  2. use the scatter command.
  3. if you use NaNs, plotdoes not know what to plot and plots nothing (but also won't connect non-existing points with lines)
  4. use a loop and plot connected sections as separate lines in the same axes

options 1/2 are the easiest if you want to do almost no changes on your code. Option 3 is the most proper and 4 mimics this result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.