Create scatterplot over line plot with matplotlib [duplicate] - python

This question already has answers here:
in pandas , add scatter plot to line plot
(1 answer)
Pandas: how to plot a line in a scatter and bring it to the back/front?
(1 answer)
Overlay a scatter plot to a line plot in matplotlib
(1 answer)
Closed 1 year ago.
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())

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:

Related

Mat plot predicted stock price graph [duplicate]

This question already has answers here:
How to avoid zigzag lines in matplotlib pyplot.plot?
(1 answer)
Matplotlib is graphing a 'zig zag' line when trying to graph polynomial
(2 answers)
Closed 6 days ago.
I am trying to plot some data but the graph does not come out straight across. It will zig-zag to the start or the end and is a mess.
I have this code:
plt.subplots(figsize=(25, 8))
plt.plot(X_test['Date'], prediction_df['Predicted Price'], color='green', marker='o', linestyle='solid')
plt.xlabel("Date")
plt.ylabel("Predicted Price")
plt.show()
I tried changing my x and y values assuming the values were off, but it did nothing. How can I fix the graph output?

Problem on combining bar and line plot in seaborn [duplicate]

This question already has answers here:
How to plot seaborn lineplot and barplot on the same plot with same number of y-axes tickers and both y-axes aligned at 0 in Python
(2 answers)
How to line plot timeseries data on a bar plot
(1 answer)
Problem in combining bar plot and line plot (python)
(2 answers)
Closed 7 months ago.
I am trying to combine a line plot and bar plot in seaborn. Code is as below:)
fig, ax1 = plt.subplots(figsize=(10,6))
sns.barplot(x=df_mergedpa['Day'], y=df_mergedpa['pro_mean'],hue=df_mergedpa['Strain'], ax=ax1)
ax2 = ax1.twinx()
sns.lineplot(x=df_mergedpa['Day'],y=df_mergedpa['ami_mean'],hue=df_mergedpa['Strain'],
marker='o', ax=ax1)
The plot I am getting is as above:
Why the line plot is not rendering properly. It is extending in X-Axis. I am not able to figure out why?
Dataframe looks as below:

How to overlay scatter plot and matrix plot so that they scale together? [duplicate]

This question already has answers here:
Scale matplotlib.pyplot.Axes.scatter markersize by x-scale
(1 answer)
How to adjust the marker size of a scatter plot, so that it matches a given radius? (Using matplotlib transformations)
(1 answer)
pyplot scatter plot marker size
(6 answers)
Marker and figure size in matplotlib : not sure how it works
(1 answer)
Closed 1 year ago.
I'm tying to overlay a scatter plot and a matrix plot, in such a way that the the relative sizes of the markersize and the boxes given in ax.matshow() do not change with scale.
For example, consider the following code:
import matplotlib.pyplot as plt
import numpy as np
ax = plt.axes()
mat = np.random.random((5,5))
ax.matshow(mat, aspect='equal')
ax.scatter([1], [2], marker='v', s=700)
This returns the following image:
However, let's say now I want to change the dimension of the matrix plot to (5,20). Then the code
ax = plt.axes()
mat = np.random.random((5,20))
ax.matshow(mat, aspect='equal')
ax.scatter([1], [2], marker='v', s=700)
returns the image
Note how the triangle I want to plot is now larger than the box shown in ax.matshow(). I would like them to scale together in a way that the triangle always fits within the matrix plot box. Any ideas on how to do this?

Scaling and Labelling KDE plots [duplicate]

This question already has answers here:
Matplotlib Legends not working
(4 answers)
How to set the y-axis limit
(8 answers)
Closed 1 year ago.
I plotted PDF using kdeplot. I am having a tough time to scale and label the plots.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
error = np.array([1,2,2,3,4,4,5])
error2 = np.array([3,3,4,4,4,6])
sns.kdeplot(error, color='blue',label='error')
sns.kdeplot(error2, color='red',label='error2')
plt.show()
I want the blue curve to be labelled as 'error' and red curve to be labelled as 'error2'.
Also, I want to scale the y-axis. It should be in the range of 0 to 1 with 0.1 interval. How can I achieve this?
Thanks in advance
To add a legend, just add
plt.legend()
above plt.show(). To set the limit of the axis, use
ax = plt.gca() # get current axis
ax.set_ylim([0, 1])
To set the ticks accordingly, you can use
ax.set_yticks(np.arange(0, 1.1, 0.1))
(All above plt.show())

How to put line plot and scatter plot on the same plot in? [duplicate]

This question already has answers here:
graphing multiple types of plots (line, scatter, bar etc) in the same window
(2 answers)
Python equivalent to 'hold on' in Matlab
(5 answers)
Closed 5 years ago.
I'm trying to plot scatter with over lined line plot. I have two sets of data and if I plot both of them as scatter plots it works, but if I try to plot the second one as a line graph (connected scatter plot), it won't even show.
plt.scatter(column1,column2,s=0.1,c='black')
plt.plot(column3,column4, marker='.', linestyle=':', color='r',)
(I tried using plt.scatter, I tried changing the markers and linestyle, tried without these as well and I still can't get it to work, I sometimes get the dots, but once I want them to be connected they disappear or nothing happens.)
plt.gca().invert_yaxis()
plt.show()
That's what I get:
Plot 1
matplotlib simply overlays plot commands in the called order as long as you do not create a new figure.
As an example, try this code:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
N = 100
x = 0.9 * np.random.rand(N)
y = 0.9 * np.random.rand(N)
plt.scatter(x, y, c='green')
plt.plot(np.linspace(0, 1, 10), np.power(np.linspace(0, 1, 10), 2), c= "red", marker='.', linestyle=':')
plt.gca().invert_yaxis()
plt.show()

Categories

Resources