2
day = ([ 1 , 2 , 5 , 6 , 7 , 8 , 9])
rain = ([0.6 , 0.8 , 1 , 6 , 6.5 ,7 , 4])
snow = ([ 1 , 2 , 0.5 , 7 , 8 , 9 , 10])

if I want to plot day on x axis and rain and snow on y axis, and for example have a dotted line joining for the days that are missing information how do I do this?

    plt.figure()
    plt.plot(day,rain,  marker='o')
    plt.plot(day, snow, marker='o')
    plt.show()
4
  • 1
    What do you mean days that are missing information? It seems like you have info for all your days here Commented Jun 12, 2018 at 16:08
  • If you want your markers connected by dotted line, just type plt.plot(day,rain, ':o'). The colon stands for dotted line Commented Jun 12, 2018 at 16:12
  • I mean for example for day 3 and 4 since the day does not exists, I want a dotted line joining 2 and 5 Commented Jun 12, 2018 at 16:12
  • What have tried to achieve this? Commented Jun 12, 2018 at 16:13

1 Answer 1

3

Here is a solution.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

day = ([ 1 , 2 , 3, 4, 5 , 6 , 7 , 8 , 9])
rain = ([0.6 , 0.8 , np.nan, np.nan, 1 , 6 , 6.5 ,7 , 4])
snow = ([ 1 , 2 , np.nan, np.nan, 0.5 , 7 , 8 , 9 , 10])

df = pd.DataFrame({'rain': rain, 'snow': snow}, index = day)
df.index.name = 'day'

fig, ax = plt.subplots()
line, = ax.plot(df['rain'].fillna(method='ffill'), ls = '--', lw = 1, label='_nolegend_')
ax.plot(df['rain'], color=line.get_color(), lw=1.5, marker = 'o')
line, = ax.plot(df['snow'].fillna(method='ffill'), ls = '--', lw = 1, label='_nolegend_')
ax.plot(df['snow'], color=line.get_color(), lw=1.5, marker = 'o')
plt.legend()
plt.xlabel('day')
plt.ylabel('mm')
plt.show()

enter image description here

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.