0

When I create a graph in Matplotlib it jumps from 19 to 100 in one go.

I have this data file:

{
    "temps": [
        "19.8498",
        "19.8377",
        "19.8352",
        "19.84",
        "19.8377",
        "19.8328",
        "19.8449",
        "19.8352"
    ],
    "pressure": [
        "98.815",
        "98.824",
        "98.805",
        "98.786",
        "98.795",
        "98.79",
        "98.818",
        "98.806"
    ]
}

and this code reading it and creating a graph:

import json
import matplotlib.pyplot as plt
import numpy as np

dataFile = json.load(open("Data.json", "r"))
temps = dataFile["temps"]
pressures = dataFile["pressure"]
time = np.arange(len(temps))

figure = plt.figure()
ax = figure.add_subplot(111)
ax.plot(time, temps)
ax.plot(time, pressures)
plt.show()

However when I plot this the scale ends up wrong and looks like this: broken graph]

How can I stop it jumping from 19 to 100 on the graph? I have tried using ylim(start,end) but that pushes every value down to almost 0

Thanks

  • 1
    The data values in your file are strings, not floats. Ie they are recognised as categorical values. You have to convert them into numbers before plotting. Are you sure you want to plot both temperature and pressure in the same axis, at the same scale, though? – zeeMonkeez Dec 11 '22 at 22:05

1 Answers1

0

You can add the following line to your code:

ax.set_ylim(min(min(temps), min(pressures)), max(max(temps), max(pressures)))

With set_ylim() you can define the max and min values of the y-axis. This stretches the y-axis to cover the whole range, so you don't get any jumps in between.

Gandhi
  • 346
  • 2
  • 9