So I'm quite new to python and I have to create a scatterplot on top of a line plot which I already made using climate data. I already have the dataframe for the scatterplot, which consists of monthly average temperatures for a station between 1837 and 2020. The line plot shows three graphs describing the mean, min and max temperatures of the period, with the x-axis displaying the months and the y-axis displaying temperature in degrees celsius. Could anyone please help me which code to use to add the scatterplot on top of the line plot? (I'm guessing by using plt.scatter())
Asked
Active
Viewed 385 times
-1
-
I think you are just looking for `plt.plot`: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html – ramzeek Oct 24 '21 at 18:26
1 Answers
0
you would need to plot the scatter and line plots on the same figure, as follows:
import random
import matplotlib.pyplot as plt
fig= plt.figure(figsize=(4, 3))
ax = plt.axes()
ax.scatter([random.randint(1, 200) for i in range(100)],
[random.randint(1, 200) for i in range(100)])
ax.plot([1, 200], [1,200])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Scatter and Line plots')
plt.show()
which would in turn return the below plot:

user120768
- 145
- 1
- 6
-
Ok great! And how do I set a nominal scale for the x-axis? Should be "jan, feb, mar, apr ..." etc. – mm_geog_24 Oct 24 '21 at 17:59
-
use positions=(range(12)) labels=(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep', 'Oct','Nov', 'Dec']) plt.xticks(positions,labels) – user120768 Oct 24 '21 at 18:22
-
if the answer has helped you and solved your problem, please accept it as the answer to your question – user120768 Oct 24 '21 at 18:25