I have a treemap that has small enough sections that the labels overlap. Is there any way to move the labels for sections under size=4 (or something around that) to either outside of the plot with an arrow pointing to it, or into a small legend only containing the labels for the small portions?
The treemap generated and code is below.
import squarify #pip install squarify
import matplotlib.pyplot as plt
labels=["longlabel1","longlabel2","longlabel3","longlabel4","longlabel5","longlabel6","longlabel7","longlabel8","longlabel9","longlabel10","longlabel11","longlabel12",]
sizes=[1.8,1.3,10.5,13.8,7.8,6.7,9.9,12.2,12.7,10.9,7.6,4.8]
x=dict(zip(labels,sizes))
sortedDict=dict(sorted(x.items(),key=lambda item:item[1],reverse=True))
squarify.plot(sizes=list(sortedDict.values()),color=['red','blue','cyan','black','gray','green'],label=list(iter(sortedDict)),alpha=.8)
plt.axis('off')
plt.show
Maybe you can use the matplotlib-extra package, which includes a treemap function to plot a hierarchical treemap.
For your case, it's simple:
import matplotlib.pyplot as plt
import mpl_extra.treemap as tr
labels=["longlabel1","longlabel2","longlabel3","longlabel4","longlabel5",
"longlabel6","longlabel7","longlabel8","longlabel9","longlabel10",
"longlabel11","longlabel12",]
sizes=[1.8,1.3,10.5,13.8,7.8,6.7,9.9,12.2,12.7,10.9,7.6,4.8]
fig, ax = plt.subplots(figsize=(7,7), dpi=100, subplot_kw=dict(aspect=1.156))
tr.treemap(ax, sizes, labels=labels,
fill=labels, cmap=['red','blue','cyan','black','gray','green'],
rectprops=dict(ec='w'),
textprops=dict(c='w'))
ax.axis('off')
The following is the obtained figure:
Related
I am using matplotlib to plot multiple curves (time series) in one plot. To do this, I use a for loop as seen below.
%matplotlib
for i in range(0, len(force)):
plt.plot(distance, (force[i]), alpha=0.1)
plt.xlabel('Distance [mm]', fontsize=12)
plt.ylabel('Force [N]', fontsize=12)
Unfortunately, with the number of curves (approx. 70) that I have, the plot would be unreadable if I labeled each curve. Does anyone know of a way to create labels that only appear when the cursor hovers in the vicinity of that curve (timeseries)?
I looked on the example from this post, but have no clue how to adapt it to my issue:
Possible to make labels appear when hovering over a point in matplotlib?
You could use mplcursors. Each curve can have a unique label, which is shown by default.
from matplotlib import pyplot as plt
import mplcursors
import numpy as np
force = np.random.randn(70, 100).cumsum(axis=1)
force -= force.mean(axis=1, keepdims=True)
plt.figure(figsize=(12, 5))
for i in range(len(force)):
plt.plot(force[i], alpha=0.2, label=f'force[{i}]')
plt.margins(x=0.01)
cursor = mplcursors.cursor(hover=True)
plt.show()
If you're working with a Jupyter notebook, you might need %matplotlib nbagg or %matplotlib qt instead of %matplotlib inline to enable interactivity.
I finished analyzing my data and want to show that they are statistically significant using the t-test_ind. However, I haven't found anything functional to show this other than what was referenced in (How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?):
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from statannot import add_stat_annotation
ax = sns.barplot(x=x, y=y, order=order)
add_stat_annotation(ax, data=df, x=x, y=y,
boxPairList=[(order[0], order[1]), (order[0], order[2])],
test='t-test_ind',
textFormat='star',
loc='outside')
Using this approach however, whenever I try to save the plot using plt.savefig() the added significancies using the add_stat_annotation are discared (matplotlib does not seem to recognize the added annotations). Using the loc='inside' option messes up my plot so it isn't really an option.
I am therefore asking if there is some simpler way to add the sigificancies directly in matplotlib / seaborn or if you can plt.savefig() with enough border / padding to include everything.
It was mainly a xlabel cut off problem. So in future applications I would use the add_stat_annotation from webermarcolivier/statannot. To save your files use one of the following possibilities:
import matplotlib.pyplot as plt
plt.tight_layout() # Option 1
plt.autoscale() # Option 2
plt.savefig('filename.png', bbox_inches = "tight") # Option 3
Hope this will help someone for future use.
I am trying to plot data to a figure and respective axis in matplotlib and as new work comes up, recall the figure with the additional plot on the axis:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
x=np.arange(0,20)
y=2*x
fig,ax=plt.subplots()
ax.scatter(x,x)
ax.scatter(x,y)
fig
Which works fine with matplotlib, if I however use seaborn's regplot:
fig2,ax2=plt.subplots()
sns.regplot(x,x,ax=ax2,fit_reg=False)
sns.regplot(x,y,ax=ax2,fit_reg=False)
fig2
fig2 generates the figure that I want but the regplot command generates an empty figure. Is there a way to suppress the regplot's empty output or have it display the updated ax2 without recalling fig2?
It seems you are using the jupyter notebook with the inline backend. In some circumstances regplot triggers the creation of a new figure even if the artists are being added to the previous one and this messes up the output. I don't know why this happens but I found a workaround that might help you, using plt.ioff to temporarily disable automatic display of figures.
plt.ioff()
fig, ax = plt.subplots()
sns.regplot(x, x, ax=ax)
fig
sns.regplot(x, 2 * x, ax=ax)
fig
You have to call plt.ioff before creating the figure for this to work. After that you have to explicitly display the figure. Then you can call plt.ion to restore the default behaviour.
regplot does not generate an empty figure. According to the documentation:
Understanding the difference between regplot() and lmplot() can be a
bit tricky. In fact, they are closely related, as lmplot() uses
regplot() internally and takes most of its parameters. However,
regplot() is an axes-level function, so it draws directly onto an axes
(either the currently active axes or the one provided by the ax
parameter), while lmplot() is a figure-level function and creates its
own figure, which is managed through a FacetGrid.
When I do the following:
fig2,ax2 = plt.subplots()
same_fig2 = sns.regplot(x,x,ax=ax2,fit_reg=False)
same_fig2.figure is fig2
>>> True
seaborn is a beautiful Python package that acts, for the most part, as an additional layer on top of matplotlib. However, it changes, for instance, things that would be matplotlib methods on a plot object to direct seaborn functions.
seaborn's despine() remove any spines (the outer edges of the plot) from a plot. But I cannot do the opposite.
I cannot seem to recreate the spine in the standard way that I would / could if I had used matplotlib entirely from the start. Is there a way to do that? How would I?
Below is an example. Could I, for instance, add a spine on the bottom and the left of the plot?
from sklearn import datasets
import pandas as pd
tmp = datasets.load_iris()
iris = pd.DataFrame(tmp.data, columns=tmp.feature_names)
iris['species'] = tmp.target_names[tmp.target]
iris.species = iris.species.astype('category')
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
sns.boxplot(x='species', y='sepal length (cm)', data=iris_new)
plt.show()
Thanks for all the great comments! I knew some of what you wrote, but not that both the 'axes.linewidth' and 'axes.edgecolor' needed to be set.
I'm writing an answer here, since it is a compilation of a few comments.
That is, the following code generates the plot below:
sns.set_style('darkgrid', {'axes.linewidth': 2, 'axes.edgecolor':'black'})
sns.boxplot(x='species', y='sepal length (cm)', data=iris_new)
plt.show()
I am using Seaborn to plot some data in Pandas.
I am making some very large plots (factorplots).
To see them, I am using some visualisation facilities at my university.
I am using a Compound screen made up of 4 by 4 monitors with small (but nonzero) bevel -- the gap between the screens.
This gap is black.
To minimise the disconnect between the screen i want the graph backgound to be black.
I have been digging around the documentation and playing around and I can't work it out..
Surely this is simple.
I can get grey background using set_style('darkgrid')
do i need to access the plot in matplotlib directly?
seaborn.set takes an rc argument that accepts a dictionary of valid matplotlib rcparams. So we need to set two things: the axes.facecolor, which is the color of the area where the data are drawn, and the figure.facecolor, which is the everything a part of the figure outside of the axes object.
(edited with advice from #mwaskom)
So if you do:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn
seaborn.set(rc={'axes.facecolor':'cornflowerblue', 'figure.facecolor':'cornflowerblue'})
fig, ax = plt.subplots()
You get:
And that'll work with your FacetGrid as well.
I am not familiar with seaborn but the following appears to let you change
the background by setting the axes background. It can set any of the ax.set_*
elements.
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
m=pd.DataFrame({'x':['1','1','2','2','13','13'],
'y':np.random.randn(6)})
facet = sns.factorplot('x','y',data=m)
facet.set(axis_bgcolor='k')
plt.show()
Another way is to set the theme:
seaborn.set_theme(style='white')
In new versions of seaborn you can also use
axes_style() and set_style() to quickly set the plot style to one of the predefined styles: darkgrid, whitegrid, dark, white, ticks
st = axes_style("whitegrid")
set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8})
More info in seaborn docs