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())
1 Answer
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:
3 Comments
mm_geog_24
Ok great! And how do I set a nominal scale for the x-axis? Should be "jan, feb, mar, apr ..." etc.
user120768
use positions=(range(12)) labels=(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep', 'Oct','Nov', 'Dec']) plt.xticks(positions,labels)
user120768
if the answer has helped you and solved your problem, please accept it as the answer to your question

plt.plot: matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html