0

I am learning matplotlib.

I am trying to plot two below plots in a single plot using matplotlib.

enter image description here

enter image description here

But it overlaps. enter image description here

Here is my code.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

train_error = [0.26462888486225206, 0.26462383329393313, 0.2628674962680674, 0.2553700231555298, 0.17473717177688022, 0.14773444580059242, 0.1468299949185866, 0.1468235689407127, 0.1439370366766204]
test_error = [0.8438224756653776, 0.8442034650577578, 1.018608707726192, 4.853704454584892, 123.69312582226338, 798.4569874115062, 3205.5264038946007, 9972.587330411312, 10787335.618580218]

plt.plot(train_error)
plt.plot(test_error)
plt.show()

Where am i doing wrong ? Can anyone please guide / help ?

5
  • Do you want two separate plots in one figure (like your first result, but grouped together)? Or so you want both lines to appear in a single graph (like your second result, but with vertical space between the lines)? If the second is what you want, then you should probably use a log scale or use a different y axis for the second line. test_error has values from 0 to 1e7 (10,000,000) and train_error has values from 0.14 to 0.27. If you plot them with a single, linear y axis, train_error and most of test_error will look like zero, as shown in your second example. Commented Apr 8, 2021 at 22:39
  • When you are plotting values over such a wide range, you will probably do best using a log scale. Otherwise variations in the small values become invisible, as shown in both of your plots of test_error. Commented Apr 8, 2021 at 22:41
  • @MatthiasFripp I want like second result. Could you please guide how to logscale? Commented Apr 8, 2021 at 22:43
  • 1
    I think you can just use plt.yscale('log') as shown at matplotlib.org/stable/tutorials/introductory/…. Commented Apr 8, 2021 at 22:48
  • @MatthiasFripp Yes, this works. Tqsm. Commented Apr 8, 2021 at 22:59

1 Answer 1

1

Use the subplot

Go check https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html

plt.subplot(1,2,1)
plt.plot(train_error)
plt.subplot(1,2,2)
plt.plot(test_error)

in plt.subplot(a,b,x) you have a,b that represents the number of (row and column) you want vertically and horizontally and x the index of the subplot selected counting from left to right and top to bottom.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you so much for helping me. But unfortunately it does not work.
Ho my bad, change the 2nd plt.subplot(1,2,1) by plt.subplot(1,2,2). And if the look doesn't please you you can make them stack vertically with plt.subplot(2,1,x) instead
this plots two plots side by side. i want in a one plot. i can see my train_erro &est_error values are sparse. do i need to normalize them ?
The sharex example in that documentation is probably a good solution for this problem.
@MatthiasFripp again it is another plot just below to first one. but i need both train_error & test_error in only one figure.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.