How to Only plot Confidence Interval in Seaborn Line Plot - python-3.x

Is there a way to only plot the confidence interval using seaborn lineplot (or another seaborn method)? For example, I used seaborn lineplot to produce this graph:
To produce this graph I just have something like:
sns.lineplot(data=df, ax=x, x='day_of_week', y='y_variable', color='lightgrey'
Since I have a lot of data points per "day of week" which is "Monday", "Tuesday", etc. I just wanted to use the default 95% CI. Is there a way to just plot the CI without the darker line in the middle (which I assume is the mean)?

You can use linewidth=0:
tips = sns.load_dataset('tips')
sns.lineplot(data=tips, x='day', y='total_bill', linewidth=0)
Output:

Related

How to add two different labels to legend for an overlap plot using matplotlib in python?

Okay this might be easy even I search on web but could not get it. Basically i want to add the two different labels to my plot and this is my line of code for that
plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data") # plot the data
plt.legend(loc="upper left")
but I am getting following result in which has same labels for both the plot.as following
Even I tried this
plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data",label="Linear Regression") # plot the data
but give the error:
SyntaxError: keyword argument repeated
This link guide for the simple way but here plt.plot()had used twice in the accepted answer.My question is how can i do it the same thing in single line code as I did in my code ?
You need two lines. One for the plot, one for the legend.
plt.plot(x[:,1], y, 'ro', x[:,1], Line_fit, 'b', linewidth=0.5, markersize=4)
plt.legend(["training data", "Linear Regression"], loc="upper left")

How can I smooth my seaborn heatmap plot in Python 3.6.0?

I have a seaborn heatmap plot like this:
With source code:
temp_cmap = ["#0416FF", "#0094FF", "#00DAFF", "#006600", "#00A305", "#71E507", "#DBF400", "#FFD602",
"#FF9B0F", "#FF1E01"]
sns.heatmap(df_grid, annot=False, cmap=temp_cmap, ax=self.axes_surface, cbar_ax=self.cbar_ax_1,
vmin=min_data,
vmax=max_data
)
But I want smooth this plot like this:
How can I do that?
It looks like you are misusing the seaborn heatmap:)
The seaborn heatmap is just a colored table. Your desired result looks like you are trying plot things on a map. There are better ways of doing that. Try something like geopandas, geoplot or folium (see a folium heatmap example here).

Main figure legend outside of subplots

I have a number of subplots within a single figure. Each figure plots multiple lines that represent the same thing (represented by color) but in different situations (different subplots). I would like to create a legend at the base of the figure showing what the color of the line means. However, I running into a problem with getting the legend to not overlap the subplots and if I can adjust the axes, getting the legend to save.
I have tried a few different solutions with some help here but have been unable to adapt to subplots. Below is an example code that I am working with.
import numpy as np
import matplotlib.pyplot as plt
m1=1
m2=10
x=np.linspace(0,100,num=101,endpoint=True)
y1m1=m1*x**2
y2m1=m1*x**0.5
y1m2=m2*x**2
y2m2=m2*x**0.5
fig=plt.figure(figsize=(4,4))
ax1=fig.add_subplot(211)
ax1.plot(x,y1m1,'b',label=r'$x^2$')
ax1.plot(x,y2m1,'r',label=r'$\sqrt{x}$')
ax2=fig.add_subplot(212)
ax2.plot(x,y1m2,'b')
ax2.plot(x,y2m2,'r')
fig.legend(loc='lower center',ncol=2)
fig.tight_layout()
fig.savefig('examplefig.png',dpi=300)
plt.show()
My goal is to save the output to a png for a good figure.
This is one way of doing it using the suggestion provided here. The idea is to add the legend at position with respect to a given axis object. In your case, since you want to add the legend at the base, it is preferred you specify the position relative to ax2. Using ncol=2 is a matter of personal choice.
fig=plt.figure(figsize=(4,4))
ax1=fig.add_subplot(211)
l1, = ax1.plot(x,y1m1,'b')
l2, = ax1.plot(x,y2m1,'r')
ax2=fig.add_subplot(212)
ax2.plot(x,y1m2, 'b')
ax2.plot(x,y2m2, 'r')
ax2.legend(handles = [l1,l2] , labels=[r'$x^2$', r'$\sqrt{x}$'],
bbox_to_anchor=(0.7, -0.2), ncol=2)
fig.tight_layout()

How to not display the pearson's coefficient on seaborn's jointplot?

I'm trying to generate a joint plot from the seaborn library, and I was wondering whether anyone knew how to not display the pearson's coefficient on the plot as it seems to be the default, as shown here:
The code that I'm using the generate the plot is shown below;
g = sns.jointplot(x=pdf['embedding 1'], y=pdf['embedding 2'],
data=pdf, kind="kde", color="m")
g.plot_joint(plt.scatter, c="w", s=0.1, marker="o")
g.ax_joint.collections[0].set_alpha(0)
plt.show()
EDIT: Updating to seaborn version 0.9.0 made it work (I was running version 0.8.1)
I'm not entirely sure whether your problem was fixed by upgrading to 0.9.0 as per your comment, but I also don't think the legend would magically disappear when doing the upgrade.
To remove the pearson's coefficient, add:
g.ax_joint.legend_.remove()
before plt.show()
I had also same problem with seaborn version 0.9.0
I couldn't understand the reason but I sold the problem with adding pearsonr score to the plot by;
import scipy.stats as stats
g.annotate(stats.pearsonr)
plt.show()

Adding a line to a Pandas plot

I'm currently tracking my internet speed and want to generate a plot of my measurements with a Timestamp, Upload value and Download value.
I'm using this to create the plot
df.plot(
kind='line',
x=timestamp_column_name,
y=[download_column_name, upload_column_name],
figsize=(12,5)
)
Generated plot:
Now I would like to add a line to this plot with the constant height of y=100000 but I can't figure out how to do this correctly. How should I do this with Pandas?
You can use axhline. Since df.plot() is a wrapper for matplotlib and returns the Matplotlib axes, which contain all the methods for interacting with the plot, it can be used straight forward as:
ax = df.plot( ... )
ax.axhline(y=100000)

Resources