1

I am very new to python and I am stuck at something. Below is the piece of code which plots two lists against each other :

plt.figure(figsize=(24, 16), dpi= 80)
for item in temperature:
  plt.plot(timestamp,item)
plt.legend(final_cities, loc = 'best')
plt.xlabel('Date Time Stamp')
plt.ylabel('Temperature in Kelvin')
plt.show()

timestamp and temperature lists looks like:

temperature=[[301.68, 300.71, 299.44, 299.6, 299.95], [306.41, 305.51, 303.36, 301.83, 303.37, 307.39]]
timestamp=['2020-09-22 15:00:00', '2020-09-22 18:00:00', '2020-09-22 21:00:00', '2020-09-23 00:00:00', '2020-09-23 03:00:00']

My code is plotting 2 graphs on top of each other. I want to print each plot as a subplot. How do I do it? Note: Each subplot will belong to a different country whose details are stored in a different list.

1

1 Answer 1

1

Is this what you are looking for?


# data
x_data = [some list]
y_data = [some list]

#  x * y should be >= len(x_data) (the amount of subplots)
fig, axs = plt.subplots(x, y)

# use a counter to get elements from your list you want to plot
c = 0
for i in range(x):
    for j in range(y):
        axs[i][j].plot(x_data[c], y_data[c])
        c += 1

    if c > len(x_data):
        break

plt.show()
Sign up to request clarification or add additional context in comments.

Comments

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.