I am trying to plot a swarplot on top of a violin plot.
Is there any way to make the swarm width to be shortened just like the width option from violin plot?
Would it be easier to use a matplotlib.scatter to do it instead of seaborn.swarmplot?
import seaborn as sns
data = pd.read_csv('allparticles.csv')
b = sns.swarmplot(x="capsid", y="dT",hue="media",data=dataT,dodge=True,size=8)
c = sns.violinplot(x="capsid", y="dT",hue="media",inner="box",data=data ,width=0.3)
This results in something like this:
I would like to make the swarmplot slimmer to match the violins.
My only other idea is to get the x min and max from the violin and plot it using matplotlib.
Thank you.
The point of the swarm plot is to displace the points so that they don't overlap. You can see for example in the WT swarm plot that the width of the swarm is determined by the number of points which are close together, plus the width of each point. If you want the plots to be slimmer, you will have to make the points smaller. You can do this using the size parameter of sns.swarmplot.
We can make it similar by reducing the size of the points so that they match with the violin plot.
Try to vary the value of the size parameter:
b=sns.swarmplot(x="capsid",y="dT",hue="media",data=dataT,dodge=True,size=3,color="0.25")
Related
graph
how do I make this graph infill all the square around it? (I colored the part that I want to take off in yellow, for reference)
Normally I use two methods to adjust axis limits depending on a situation.
When a graph is simple, axis.set_ylim(bottom, top) method is a quick way to directly change y-axis (you might know this already).
Another way is to use matplotlib.ticker. It gives you more utilities to adjust axis ticks in your graph.
https://matplotlib.org/3.1.1/gallery/ticks_and_spines/tick-formatters.html
I'm guessing you're using a list of strings to set yaxis tick labels. You may want to set locations (float numbers) and labels (string) of y-axis ticks separatedly. Then set the limits on locations like the following snippet.
import matplotlib.pyplot as plt
import matplotlib.ticker as mt
fig, ax = plt.subplots(1,1)
ax.plot([0,1,2], [0,1,2])
ax.yaxis.set_major_locator(mt.FixedLocator([0,1,2]))
ax.yaxis.set_major_formatter(mt.FixedFormatter(["String1", "String2", "String3"]))
ax.set_ylim(bottom=0, top=2)
It gives you this: generated figure
Try setting the min and max of your x and y axes.
Is there any way that we can use color intensities instead of bubble size for a weighted scatter plot? I have been searching for solutions online for hours, but I still did not find one. I use the following Penguins data for illustration.
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
penguins_data="https://raw.githubusercontent.com/datavizpyr/data/master/palmer_penguin_species.tsv"
penguins_df = pd.read_csv(penguins_data, sep="\t")
sns.set_context("talk", font_scale=1.1)
plt.figure(figsize=(10,6))
sns.scatterplot(x="culmen_length_mm",
y="culmen_depth_mm",
size="body_mass_g",
sizes=(20,500),
alpha=0.5,
data=penguins_df)
# Put the legend out of the figure
plt.legend(bbox_to_anchor=(1.01, 1),borderaxespad=0)
# Put the legend out of the figure
#plt.legend(bbox_to_anchor=(1.01, 0.54), borderaxespad=0.)
plt.xlabel("Culmen Length (mm)")
plt.ylabel("Culmen Depth (mm)")
plt.title("Bubble plot in Seaborn")
plt.tight_layout()
plt.savefig("Bubble_plot_size_range_Seaborn_scatterplot.png",
format='png',dpi=150)
The bubble plot with the smallest bubble corresponding to the smallest body mass and the biggest bubble corresponds to the largest body mass. However, I need the color intensity for the weighted scatter plot. For example, a darker color indicates that it occurs more frequently, and a lighter color indicates that it occurs less frequently. Any suggestion using Stata (preferred), Python, or R is highly appreciated.
I found something in Stata like this one, but my data structure is completely different, so it does not work out.
Have you considered creating a new column in your dataframe for the color, where you adjust the alpha channel by yourself?
After that you can probably work from this question to use it as the color column as the hue for the markers.
I used the swarmplot function in seaborn to plot the category scatter plot. But I have two types of points in each category. So I wish can set different types of points into different markers.
Can the swarmplot of seaborn adjust the marker shape of points? Or I can use other tools
I only found the parameter markers size can be adjusted in the document of the swarmplot. And I tried to use hue. But when I use hue, all categories represent same color. That is not my idea.
3.
# plot the manhattan map
snsplt =sns.swarmplot(x=scale,y=distance,marker='o')
plt.tick_params(labelsize=12)
# plot the significant line
bin = np.arange(-0.2,5.2,0.2)
y = np.full((len(bin),),distance[p_minsignIndex])
snsplt = sns.lineplot(x=bin,y=y)
plt.show()
The image is my draft.
I wish the circles above the line are filled circles and the circles below the line are hollow circles.
Given the plots you show, it seems that you have two categories happening at the same time:
1) the position along the axis indicates one category -- I'll call this cat1; and,
2) variation within each category and this is what you want to show by the marker -- I'll call this cat2.
By far, the easiest ways to show these two categories together is to use the tools given to you by seaborn to do this. Specifically, in your plots, you are identifying cat1 in two different ways: first by its position along the x-axis, and second by the color. So the idea is to use the position for cat1 and the color for cat2. Also, you mention in the comments below that you want to use the markers to show a statistical significance, so I chose a palette that does that well. Here's an example taken from the seaborn docs, but modified to show a significance threshold (as you requested):
import seaborn as sns
import numpy as np
tips['bigness'] = np.where(tips['total_bill']>15, 'big', 'small')
sns.swarmplot(x="day", y="total_bill", hue="bigness", data=tips, palette="Paired", hue_order=["small", "big"])
corrmat is correlation dataframe with 37 columns and 37 rows
Code:
f, ax = plt.subplots(figsize=(30,25))
sns.heatmap(corrmat,vmax=0.8,square=True)
I am not able to change the rotation of labels and it is creating mess as no. of variables used for correlation matrix is more in number.
Let me know how to make below heatmap more readable
Heatmap(37*37)
If you have also done an import matplotlib.pyplot like this:
import matplotlib.pyplot as plt
You can specify the following code after you create the heatmap to set the degree of label rotation of both y and x axis.
plt.yticks(rotation= 0)
plt.xticks(rotation=90)
You can play around with the exact number of rotation until you are happy with how it looks.
Seaborn is built on top of the matplotlib library. So, to rotate labels, you'll need to access the axis object and rotate it.
Something like this might work:
for tick in ax.get_xticklabels():
tick.set_rotation(45)
You can similarly rotate y-axis labels and calibrate the rotation angles using the number.
Let's look at a swarmplot, made with Python 3.5 and Seaborn on some data (which is stored in a pandas dataframe df with column lables stored in another class. This does not matter for now, just look at the plot):
ax = sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current, hue=self.dte.label_voltage, data = df)
Now the data is more readable if plotted in log scale on the y-axis because it goes over some decades.
So let's change the scaling to logarithmic:
ax.set_yscale("log")
ax.set_ylim(bottom = 5*10**-10)
Well I have a problem with the gaps in the swarms. I guess they are there because they have been there when the plot is created with a linear axis in mind and the dots should not overlap there. But now they look kind of strange and there is enough space to from 4 equal looking swarms.
My question is: How can I force seaborn to recalculate the position of the dots to create better looking swarms?
mwaskom hinted to me in the comments how to solve this.
It is even stated in the swamplot doku:
Note that arranging the points properly requires an accurate transformation between data and point coordinates. This means that non-default axis limits should be set before drawing the swarm plot.
Setting an existing axis to log-scale and use this for the plot:
fig = plt.figure() # create figure
rect = 0,0,1,1 # create an rectangle for the new axis
log_ax = fig.add_axes(rect) # create a new axis (or use an existing one)
log_ax.set_yscale("log") # log first
sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current, hue=self.dte.label_voltage, data = df, ax = log_ax)
This yields in the correct and desired plotting behaviour: