0

I was trying to do a comparison of runtime between Naive matrix multiplication and Strassen's. For this, I was recording the runtime for a different dimension of the matrices. Then I was trying to plot the result in the same graph for the comparison.

But the problem is the plotting is not showing the proper result.

  1. Here is the data... 2 3142 3 3531 4 4756 5 5781 6 8107

The leftmost column is denoting n, the dimension and rightmost column is denoting execution time.

The above data is for Naive method and the data for Strassen is in this pattern too.

I'm inserting this data to a pandas dataframe. And after plotting the data the image looks like this: enter image description here

Here blue is for Naive and green is for Strassen's This is certainly not true as Naive cannot be constant. But my code was correct. SO I decided to plot them separately and these are the result:

Naive Naive

Strassen enter image description here

As you can see it might happen because the scaling in Y axis is not the same? Is this the reason?

The code I'm implementing for plotting is:

fig = plt.figure()

data_naive = pd.read_csv('naive.txt', sep="\t", header=None)
data_naive.columns = ["n", "time"]
plt.plot(data_naive['n'], data_naive['time'], 'g')

data_strassen = pd.read_csv('strassen.txt', sep="\t", header=None)
data_strassen.columns = ["n", "time"]
plt.plot(data_strassen['n'], data_strassen['time'], 'b')

plt.show()

fig.savefig('figure.png')

What I tried to work out?

fig = plt.figure()

data_naive = pd.read_csv('naive.txt', sep="\t", header=None)
data_naive.columns = ["n", "time"]

data_strassen = pd.read_csv('strassen.txt', sep="\t", header=None)
data_strassen.columns = ["n", "time"]

ax = data_naive.plot(x='n', y='time', c='blue', figsize=(20,10))
data_strassen.plot(x='n', y='time', c='green', figsize=(20,10), ax=ax)

plt.savefig('comparison.png')
plt.show()

But no luck!!!

How to plot them in the same figure without altering their actual orientation?

Abbas
  • 3,872
  • 6
  • 36
  • 63
sphoenix
  • 3,327
  • 5
  • 22
  • 40
  • The Strassen values are 1000 times higher (1e9 vs 1e7 on your graphs) which is why the naive method looks like a flat line in the first graph. – T Burgis Nov 06 '18 at 15:26
  • 1
    The two datasets are different by 2 orders of magnitude. So the question is; how would you like to plot them? Would you like to use two different scales? Would you like to normalize the values to show them on the same scale? – ImportanceOfBeingErnest Nov 06 '18 at 15:27
  • Which one is better for comparison? @ImportanceOfBeingErnest – sphoenix Nov 06 '18 at 15:28
  • 1
    For comparing runtime, the graph you already have is pretty good. Or you can show that on a log scale (`plt.gca().set_yscale("log")`). For comparisson of the shape, two scales might be better, `ax2 = plt.gca().twinx()`. – ImportanceOfBeingErnest Nov 06 '18 at 15:51

1 Answers1

-1

IIUC: Here is a solution using twinx

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randint(10, 100, (12,2)))
df[1] = np.random.dirichlet(np.ones(12)*1000., size=1)[0]

fig, ax1 = plt.subplots()
ax1.plot(df[0], color='r')
#Plot the secondary axis in the right side
ax2 = ax1.twinx()
ax2.plot(df[1], color='k')
fig.tight_layout()
plt.show()

Result produced: enter image description here

Abbas
  • 3,872
  • 6
  • 36
  • 63
  • Thanks for your answer but I think I'll go with my segmented image. Anyway, I didn't give the downvote. – sphoenix Nov 07 '18 at 14:14