How to plot pandas dataframe in same figure without destroying their actual orientation?

90 Views Asked by At

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?

1

There are 1 best solutions below

1
On

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