Slow matplotlib (Python) to plot a 2-D graph?

Asked

Viewed 180 times

0

Lately I came up with the need to use Python for plotting graphics with more than 3600 coordinates, but I realized that time can be a problem, but I’m not sure if the code I did has any performance problem or if it is from the 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()
  • As for this part: for k in range(Len(tempo)): plt.Plot(tempo,tensao, color='black') What are you doing with this? what is the purpose of this part of the code?

1 answer

2


If we consider that the object tempo is an eternal 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 so tempo how much tensao are eternal (list), you just make the direct call to the function plot passing the two objects as parameter:

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

Addendum

Even being unnecessary in the actual solution of the problem, in Python, you never make the following structure:

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

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

for i in X:
    ...

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

  • Thank you very much, my Python walks well rusty, strong hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.