1

I need to plot some data with basic line graph.

       datatime             col1
29/08/2017 10:13:23.972      NaN
29/08/2017 10:13:23.972      NaN
29/08/2017 10:13:54.449      425
29/08/2017 10:14:19.800      425
29/08/2017 10:14:45.232      425
29/08/2017 10:14:48.567      NaN
29/08/2017 10:15:19.331      2
29/08/2017 10:15:38.081      380
29/08/2017 10:16:27.517      380

Now I have some problems. What I need is to plot this and if there is NaN to create a blank space on the graph, not a line from previous to next value. I used to plot with matplotlib but don't know how to integrate that "skip" step. And it would be good if it's possible to plot value on line for each jump higher or lower.

What I have:

def plot_1(data, par_time, par_y, par_ylabel, par_legend):
    fig = plt.figure(figsize=(5, 3))
    ax1 = fig.add_subplot(111)
    plt.gca().set_color_cycle(['red', 'blue', 'green', 'brown'])
    ax1.plot(data[par_time], data[par_y], label=par_ylabel, marker='o', linewidth=2.0)
    plt.ylabel(par_ylabel)
    handles, labels = ax1.get_legend_handles_labels()
    ax1.legend(handles, par_legend, loc='upper center', bbox_to_anchor=(1.15, 1))
    ax1.grid('on')
    for i, j in zip(data[par_time], data[par_yticks]):
        ax1.annotate(str(j), xy=(i, j))
    a = plt.savefig('/home/user/graph.png)
    return a

Here is not so much rows, but if it would be, x would be so congested, is it possible to create each grid on 3 seconds for example?

Thank for any help, in advance.

3
  • What is your question? Changing the grid? The number of x labels? How to interrupt a line plot, when the datapoint is NaN? And are you able to plot your figure just using the information provided in the question, i.e., is this a Minimal, Complete, and Verifiable example? Commented Jun 4, 2018 at 15:24
  • My question is how to skip NaN values, how to stay blank on graph where is NaN values, not a line to the next value. That's one of my questions, the one that makes me the biggest problem. Commented Jun 4, 2018 at 15:30
  • 1
    Matplotlib automatically interrupts the line, where a NaN value is. To overcome this, is topic of some question on SO. So I don't know, why you need specific code for it. Commented Jun 4, 2018 at 16:07

1 Answer 1

1

IIUC, this code could work for you. I set the x-axis interval to 30 seconds (instead of 3, which you had asked for), because 3-second interval leads to a lot of crowding of your x axis. In any case, it should give you an idea of how to move forward.

The basic idea in this code to create a gap where you have NaN values is to create a new column in your data, in order to group each continuous (non-NaN) block together, and then plot each one of those groups.

import matplotlib.pyplot as plt
import matplotlib.dates as md

# Make sure `datatime` is in datetime format
df['datatime'] = pd.to_datetime(df.datatime)

# create new group if interrupted by NaN
df['group'] = df.col1.isnull().cumsum()


fig, ax = plt.subplots()
# Groupby your new group column, and plot each group
for _, group in df.groupby('group'):
    plt.plot(group.dropna(subset=['col1']).datatime,
             group.dropna(subset=['col1']).col1,
             color='blue')

# Create your 30 second interval on the x axis, and format your dates
xlocator = md.SecondLocator(interval=30)
dateFmt = md.DateFormatter('%H:%M:%S') 

ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(dateFmt)
plt.xticks(rotation=90)

enter image description here

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

2 Comments

Hmm, this looks very good on data I provided for question, but when I run on real data, I got next error: Locator attempting to generate 55502 ticks from 736552.0711 to 736571.34228: exceeds Locator.MAXTICKS
I haven't run into that problem before, but it sounds like it just can't handle that many ticks. Maybe reconsider the interval (use a much larger interval, that way there will be way fewer ticks)

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.