1

I am trying to plot a graph on python using the function matplotlib.pyplot and it seems that when I run the codes, they apply but to separate graphs. So when I applied plt.scatter(Lt, T) it showed the graph, then when I applied plt.xlabel('Local Time (hours)') it did run but to a new graph without the data inputed when I ran put.scatter(Lt,T) so basically I get a plain graph with only an x-axis title.

import matplotlib.pyplot as plt
plt.scatter(L, T)
plt.axis([0,25,200,800])
plt.xlabel('Local Time')
plt.ylabel('Surface Temperature')
plt.title('Surface Temperature vs. Local Time')

I expect all the codes and functions to apply to the same graph.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
  • Hey, could you paste the code to help people understand. Thanks – bamdan Sep 12 '19 at 18:20
  • [Is this what you're looking for?](https://stackoverflow.com/questions/22276066/how-to-plot-multiple-functions-on-the-same-figure-in-matplotlib) – Cody Smith Sep 12 '19 at 18:27
  • Hey I've had a guess at what you are trying to do based on the code. If it answers your question can you accept please. Thanks – bamdan Sep 12 '19 at 18:33
  • Thank you all, I got it to work by highlighting all the steps together then pressing shift and enter. – Mouza AlShehhi Sep 12 '19 at 18:53

2 Answers2

1

I think you need use the show method. You usually add this at the end after you've defined the axes etc. Sometimes it makes sense to wrap it in a function like below.

import matplotlib.pyplot as plt

def plotter(L,T):
    plt.scatter(L, T)
    plt.axis([0,25,200,800])
    plt.xlabel('Local Time')
    plt.ylabel('Surface Temperature')
    plt.title('Surface Temperature vs. Local Time')
    plt.show()

plotter(L,T)

Hope that helps.

bamdan
  • 836
  • 7
  • 21
1

You can first create an matplotlib.axes.Axes and then plot on it:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(L, T)
ax.set_xlabel('Local Time')
ax.set_ylabel('Surface Temperature')
ax.set_title('Surface Temperature vs. Local Time')
Paulloed
  • 333
  • 1
  • 9