0

I have 2 Pandas series, each with indexes of DateTimeIndex and values for where the other doesn't e.g.

a = 
                     values  
2018-01-20 01:58:15  1000
2018-01-20 01:58:20  1005 
2018-01-20 01:58:25  1010
2018-01-20 01:58:45  1030
2018-01-20 01:58:50  1040


b = 
                     values
2018-01-20 01:58:30  1015 
2018-01-20 01:58:35  1020
2018-01-20 01:58:40  1025

and I want to plot both on the same graph, with different colour markers for each series, using matplotlib.

e.g.

plt.plot(xs, a.values)
plt.plot(xs, b.values)

(where xs is the combined index)

What's the best/most elegant way to do this?

3
  • 3
    At least have the courtesy to post the Series as such instead of leaving us guessing. SO is not a free coding service. Commented Aug 6, 2018 at 18:06
  • Apologies, edited to show the representative series Commented Aug 6, 2018 at 18:10
  • 2
    What's wrong with just plotting the series on the same axes? What do you mean by combined index? I have no idea what you're asking at this point. Commented Aug 6, 2018 at 18:29

2 Answers 2

1

You can iterate through a list of your series, and plot each one. Colors will be automatically assigned to each series.

import matplotlib.pyplot as plt

for data in [a,b]:
    # make sure the index is a datetime index
    data.index = pd.to_datetime(data.index)
    plt.scatter(data.index, data['values'])

# Set your x axis to be limited to your date ranges
plt.xlim(min(np.concatenate([a.index.values, b.index.values])),
         max(np.concatenate([a.index.values, b.index.values])))

# Rotate the x ticklabels
plt.xticks(rotation=90)
plt.tight_layout()

plt.show()

enter image description here

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

1 Comment

Yep this is perfect thanks, I was thinking I had to align them both to the same length index to plot onto the same axis but this works great. Much easier than I thought!
0
import matplotlib.pyplot as plt
plt.plot(a.index, a.values, b.index, b.values)

1 Comment

Please edit your answer to add an explanation of how your code works and how it solves the OP's problem. Many SO posters are newbies and will not understand the code you have posted.

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.