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?
Related
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:
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:
This question already has answers here:
Plot negative values on a log scale
(2 answers)
Negative axis in a log plot
(1 answer)
Plot logarithmic axes
(6 answers)
Closed 1 year ago.
Matplotlib appears to not be plotting several points despite me changing the x-limit based on the inputted numbers I am trying to graph. (x-limit because this is a horizontal barplot).
Here is the code:
low = min(df_grouped_underlying['Currency Exposure'].astype(float))
high = max(df_grouped_underlying['Currency Exposure'].astype(float))
fig = plt.figure()
ax = fig.add_subplot()
ax.set_xlim([math.ceil(low-0.5*(high-low)), math.ceil(high+0.5*(high-low))])
bar_1 = ax.barh(df_grouped_underlying.index, df_grouped_underlying['Currency Exposure'].astype(float), label='Currency Exposure')
ax.set(title = 'Currency_Exposure_Underlying_Position',
ylabel = 'Underlying Currency',
xlabel = 'Exposure')
plt.legend(loc="upper right")
plt.savefig('agg_exposure.png', bbox_inches='tight')
plt.show()
Here is the output:
Output of the plot
This question already has answers here:
Rotating axis text for each subplot
(3 answers)
Date ticks and rotation in matplotlib
(6 answers)
Aligning rotated xticklabels with their respective xticks
(5 answers)
Closed 2 years ago.
I have subplots with shared x,y axis. The x-axis is datetime stamp. Everything looks fine. The problem is x-axis looks clumsy.
My code:
fig, ax = plt.subplots(nrows=rows, ncols=cols,sharey=True,sharex=True)
fig.subplots_adjust(hspace=0.025, wspace=0.05)
for i in range(rows):
for j in range(cols):
#### plot data goes here.
ax[i,j].autofmt_xdate()
ax[i,j].xaxis.set_tick_params(rotation = 30)
fig.subplots_adjust(right=0.95)
plt.show()
Present output: The x-axis labels look clumsy, hard to read.
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()