-1

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

1 Answer 1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.