0

I want to create a chart with a for loop. X-axis is plotted correctly and even y-axis scale is set correctly, but no actual line chart is created. What could be wrong with the code below?

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

test = {'date':[2012, 2013, 2014, 2015],'val':[9,15,12,20]}

test_df=pd.DataFrame(data=test)

test_df=test_df.set_index('date')

fig, ax = plt.subplots(figsize=(25,25))

for i in range(0, len(test_df)):
    
    ax.plot(test_df.index[i], test_df['val'].iloc[i], color='blue', linewidth=10)
        
plt.show()

2 Answers 2

1

You could either plot the whole series with:

ax.plot(test_df.index, test_df['val'], color='blue', linewidth=10)

and no for loop

Or plot the points themselves with:

ax.plot(test_df.index[i], test_df['val'].iloc[i], "b+", linewidth=10)

See matplotlib's markers for individual points markers

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

1 Comment

Thank you, this solved the issue for me! "b+" didn't work itself, but other markers did.
1

Yo do not give coordinates to plot a line which leads us to plot a scatter plot.

for i in range(0, len(test_df)): 
    ax.scatter(test_df.index[i], test_df['val'].iloc[i], color='blue', linewidth=10)

As far as I'm concerned, this would solve your problem but if you really want to connect those dots then you might give it a shot without a for loop:

ax.plot(test_df.index, test_df['val'], color='blue', linewidth=10)

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.