Matplotlib (Python) slow to plot a 2-D chart?

0

I have recently come up with the need to use Python to plot graphics with more than 3600 coordinates, but I realized that time may be a problem, but I'm not sure if the code I've done has a performance problem or if it's from the same library:

plt.figure()

for k in range(len(tempo)):
    plt.plot(tempo,tensao, color='black')

fig1 = plt.gcf()
plt.rcParams['lines.linewidth'] = 0.5
plt.ylim([-1,2])
plt.xlim([min(tempo),max(tempo)])
plt.grid(True)
plt.savefig('resultadoPlot.jpeg', dpi=1200, facecolor='w', edgecolor='w',orientation='portrait', papertype=None, format=None,
        transparent=False, bbox_inches=None, pad_inches=0.1,
        frameon=True)
plt.show()
    
asked by anonymous 22.03.2018 / 00:09

1 answer

2

If we consider that the object tempo is an iterable of 3600 values, in the code

for k in range(len(tempo)):
    plt.plot(tempo,tensao, color='black')

You would be generating the same graph 3600 times, which is probably the reason for the slowness. If both tempo and tensao are iterable (list), you only have to call the plot function by passing the two objects as a parameter:

plt.plot(tempo, tensao, color='black')

Addendum

Even though it's unnecessary to solve the problem in Python, you never make the following structure:

for i in range(len(X)):
    ...

Because this structure is considered a language addiction and has several negative aspects when considered performance, semantics and readability. Almost always, in these cases, the solution is only:

for i in X:
    ...

But, remembering, in the case of the question, this loop of repetition is unnecessary.

    
22.03.2018 / 11:05