I have a dataframe that looks similar to:
JavaScript
x
14
14
1
df = pd.DataFrame(
2
{'id': [53, 54, 55, 56, 57],
3
'true_distance': [880.32,1278.87,838.44,6811.63,13339.92],
4
'estimated_distance': [330.23,1099.73,534.86,6692.78,6180.8]}
5
)
6
7
df
8
id true_distance estimated_distance
9
0 53 880.32 330.23
10
1 54 1278.87 1099.73
11
2 55 838.44 534.86
12
3 56 811.63 6692.78
13
4 57 13339.92 6180.80
14
I am required to give a visual comparison of true and estimated distances.
My actual df shape is:
JavaScript
1
3
1
df_actual.shape
2
(2346,3)
3
How do I show true_distance
side-by-side estimated_distance
on a plot, where one can easily see the difference in each row, considering the side of my df_actual
?
Advertisement
Answer
Here are some ways to do it.
Method1
JavaScript
1
6
1
import matplotlib.pyplot as plt
2
plt.plot(df.true_distance)
3
plt.plot(df.estimated_distance, 'o')
4
5
plt.show()
6
output
Method 2
JavaScript
1
21
21
1
import matplotlib.pyplot as plt
2
import numpy as np
3
4
5
def plotGraph(y_test,y_pred,regressorName):
6
if max(y_test) >= max(y_pred):
7
my_range = int(max(y_test))
8
else:
9
my_range = int(max(y_pred))
10
plt.scatter(range(len(y_test)), y_test, color='blue')
11
plt.scatter(range(len(y_pred)), y_pred, color='red')
12
plt.title(regressorName)
13
plt.show()
14
return
15
16
17
y_test = range(10)
18
y_pred = np.random.randint(0, 10, 10)
19
20
plotGraph(df.true_distance, df.estimated_distance, "test")
21
output
Method3
JavaScript
1
13
13
1
plt.figure(figsize=(10,10))
2
plt.scatter(df.true_distance, df.estimated_distance, c='crimson')
3
plt.yscale('log')
4
plt.xscale('log')
5
6
p1 = max(max(df.estimated_distance), max(df.true_distance))
7
p2 = min(min(df.estimated_distance), min(df.true_distance))
8
plt.plot([p1, p2], [p1, p2], 'b-')
9
plt.xlabel('True Values', fontsize=15)
10
plt.ylabel('Predictions', fontsize=15)
11
plt.axis('equal')
12
plt.show()
13