Matplotlib doesn't plot the lines on the chart

Hello, everyone!

I’m working in a personal project and I need to plot this dataframe:

What I need is chart with several plots, one for each column (except the date column that would be the x axis). By the way this data is about the exchange variation of some Latin america currencies against the United States Dollar. And I’m sorry if some words are in Portuguese, as I said it’s a personal project, and I do not think they will stop you from understanding the code.

Anyway, this is the code I’m using:

fig, ax = plt.subplots(figsize=(16, 8))

months = mdates.MonthLocator()

months_fmt = mdates.DateFormatter('%m/%Y')

ax.xaxis.set_major_locator(months)

ax.xaxis.set_major_formatter(months_fmt)

lim_i = dt.datetime.strptime(vars.iloc[0, 0], '%d/%m/%Y')

lim_f = dt.datetime.strptime(vars.iloc[-1, 0], '%d/%m/%Y')

ax.set_xlim(lim_i, lim_f + dt.timedelta(days=30))

xlim = ax.get_xlim()[1]

for i in range(1, vars.shape[1]):

    if i == 1:

        cor = 'red'

    else:

        cor = 'grey'

    ax.plot(vars['data'], vars.iloc[:, i], color=cor)

    ax.text(x=float(xlim) - 28, y=vars.iloc[-1, i], s=f'{labels[i-1]} ({vars.iloc[-1, i]}%)', alpha=0.4)

ax.tick_params(bottom=False, top=False, right=False, left=False)

ax.set_ylabel('Oscilação acumulada %')

plt.grid(False)

for key, spine in ax.spines.items():

    spine.set_visible(False)

plt.tight_layout()

#plt.savefig(fname='variação cambial2.png', format='png')

plt.show()

And this is what I’m getting:

Everything works fine but the lines are not plotted. Can anyone see what I’m doing wrong?

I got the answer in stack overflow in case anyone is interested.

Hi Otavio,
I was watching your code and wanted to ask you about the ‘data’ format. That’s what the Stack overflow answer is about.

I recommend you for next time to share both the entire code and the table, you will get better and faster answers if we can “copy, paste and run”.

Regarding the error, this things happens, all the time. You might be tired or distracted while coding. Anyway you can find techniques that will help you to avoid such mistakes. For example, instead of formatting the values using datetime for lim_i and lim_f, you can have a coding rule like “always use the variable across the file as it is”. Then you format all variables you need at the beginning, save them, and you will not touch it again (unless necessary). A good approach is to use a KISS coding style.

Yes, my bad. I should’ve posted the data just like a I did at stackoverflow, not as an image.

I definitely was tired :sweat_smile:. And also I do believe that whenever you’re writing code or text and you’ve looked at it over and over again during a couple of days, you lose the ability to find errors as your brain processes everything you are “reading” without actually reading since it already knows what’s there.

Oh that it’s so true! I formatted the values using datetime for lim_i and lim_f but forgot to do it for the rest of the column. I’ll look into techniques to avoid mistakes, starting for KISS coding style.

Thank you for your answer!