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:
Related
This question already has answers here:
Seaborn Catplot set values over the bars
(3 answers)
How to add value labels on a bar chart
(7 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm plotting a set of sns countplots with 6 different plots, and I'd like to add data labels to all of them without repeating the process. All of the other questions solve the problem for only one plot.
At the last part of the code (for p in ax.patches...) I'd expect the labels to appear on the top of the bars. But nothing happened. It works with only one plot, but not with several plots. This is the result I wanted, but for all of them at once:
This is the result I get (no data labels for any plot):
What am I doing wrong? Is there any easier way of doing it?
# creating the plots
fig, ([ax1, ax2], [ax3, ax4], [ax5, ax6]) = plt.subplots(nrows=3, ncols=2, figsize=(20,15))
sns.countplot(x='sex', data=df, order=df['sex'].value_counts().index, ax=ax1).set(title='Clients by Gender')
sns.countplot(x='age_range', data=df, ax=ax2).set(title='Clients by Age')
sns.countplot(x='children', data=df, ax=ax3).set(title='Clients by Children')
sns.countplot(x='region', data=df, order=df['region'].value_counts().index, ax=ax4).set(title='Clients by Region')
sns.countplot(x='smoker', data=df, order=df['smoker'].value_counts().index, ax=ax5).set(title='Clients by Smoker Option')
sns.countplot(x='bmi_range', data=df, order=df['bmi_range'].value_counts().index, ax=ax6).set(title='Clients by BMI')
for p in ax.patches:
height = p.get_height()
ax.text(x=p.get_x()+p.get_width()/2, y=height+20, s='{:.0f}'.format(height), ha='center')
plt.show()
This question already has answers here:
seaborn distplot / displot with multiple distributions
(6 answers)
Plotting multiple seaborn displot
(1 answer)
seaborn is not plotting within defined subplots
(1 answer)
Closed 9 months ago.
I am not experienced with plotting in Python. But I have managed to plot a signle distribution plot with Seaborn.
example code:
sns.displot(SD_frame_A,kind="kde")
example plot:
So I tryed to plot three of them in one graph:
example code:
sns.displot(SD_frame_A,kind="kde")
sns.displot(SD_frame_S,kind="kde")
sns.displot(SD_frame_D,kind="kde")
plt.show()
But this will only plot the three distribution separately. Does anyone how I can plot both 3 distribution in one plot?
Thanks for reading!
You can't do that with displot because that is a figure-level function. But you can use kdeplot and provide an axes object:
ax = plt.axes()
sns.kdeplot(SD_frame_A, ax=ax)
sns.kdeplot(SD_frame_S, ax=ax)
sns.kdeplot(SD_frame_D, ax=ax)
plt.show()
This question already has answers here:
Python Seaborn Facetgrid change xlabels
(2 answers)
Common xlabel/ylabel for matplotlib subplots
(8 answers)
How to add a shared x-label and y-label to a plot created with pandas plot
(4 answers)
One shared x-axis label for Seaborn FacetGrid subplots (layouts/spacing?)
(1 answer)
How to set common axes labels for subplots
(9 answers)
Closed last year.
What I need should be straighforward but I couldn't find a solution. Say we draw the following seaborn.lmplot:
import seaborn as sns; sns.set_theme(color_codes=True)
tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", col="day", hue="day",
data=tips, col_wrap=2, height=3)
I simply want to have a single label for the x-axis and a single label for the y-axis instead of two as currently.
In other words, that the word 'tip' be printed only one time on the centre left of the graph, and that the word 'total_bill' be printed only one time on the bottom centre of the graph.
How do we do this?
EDIT: there is a similar question here One shared x-axis label for Seaborn FacetGrid subplots (layouts/spacing?) yet it is not elaborated and does not solve my issue.
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