How to properly plot graph using matplotlib?

Question:

I have these two lists:

**l1** = ['100.00', '120.33', '140.21', '159.81', '179.25', '183.13', '202.49', '202.89', '204.18', '205.35', '206.44', '207.45', '208.40', '209.30', '210.15', '210.96', '211.73', '212.47', '213.18', '213.87', '214.53', '215.17', '215.79', '216.39', '216.98', '217.54', '218.10', '218.63', '219.16', '219.67', '220.18', '220.67', '221.15']
**l2** = ['13.14', '13.37', '13.53', '13.66', '13.76', '13.77', '20.70', '21.51', '23.85', '26.39', '29.13', '32.06', '35.17', '38.47', '41.95', '45.63', '49.50', '53.59', '57.90', '62.45', '67.25', '72.33', '77.70', '83.40', '89.43', '95.83', '102.65', '109.90', '117.65', '125.95', '134.84', '144.40', '154.71']

I plot l1 against l2 and this is what it’s supposed to come out:
should_be

I use the following code:

fig, ax = plt.subplots(1)
ax.plot(l1, l2)
plt.show()

and this is what comes out it_is
like the step is regular even if the values are not equally distributed. Thanks

Asked By: drSlump

||

Answers:

Have you tried using this?

%matplotlib inline

those graphs don’t typically show up unless you have that line above where it’s rendering.

Answered By: Christian

Have you tried to limited the views of the graph?

plt.xlim([25, 50])

or maybe, the axe range of them.
You can see more about it in this link:

https://stackabuse.com/how-to-set-axis-range-xlim-ylim-in-matplotlib/.

Answered By: lewis

The problem here is that the list contain a string of values, however they must be a list of integer/float values. I also do not think you need subplots unless you want to have more than one plot. Below I have modified and executed the same and it works:

import matplotlib.pyplot as plt

l1 = [100.00, 120.33, 140.21, 159.81, 179.25, 183.13, 202.49, 202.89, 204.18, 205.35, 206.44, 207.45, 208.40, 209.30, 210.15, 210.96, 211.73, 212.47, 213.18, 213.87, 214.53, 215.17, 215.79, 216.39, 216.98, 217.54, 218.10, 218.63, 219.16, 219.67, 220.18, 220.67, 221.15]
l2 = [13.14, 13.37, 13.53, 13.66, 13.76, 13.77, 20.70, 21.51, 23.85, 26.39, 29.13, 32.06, 35.17, 38.47, 41.95, 45.63, 49.50, 53.59, 57.90, 62.45, 67.25, 72.33, 77.70, 83.40, 89.43, 95.83, 102.65, 109.90, 117.65, 125.95, 134.84, 144.40, 154.71]

plt.plot(l1, l2)
plt.show()

Output:
enter image description here

Answered By: coldy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.