Use ipywidgets to interatively find best position matplotlib text - matplotlib

I am interested in using the interact function to use a slider to adjust the position of text in a matplotlib plot (you know, instead of adjusting the position, running the code, and repeating 1000 times).
Here's a simple example of a plot
import matplotlib.pyplot as plt
x=0.2
y=0.9
plt.text(x, y,'To move',size=19)
plt.show()
and some interact code
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
def f(x):
return x
interact(f, cx=0.2)
I'm wondering how I can combine these to generate a plot with the text along with a slider that will interactively move the text based on the specified value for x. Is this possible? What if I want to do the same for y?
Thanks in advance!

Here you go:
%matplotlib inline
import matplotlib.pyplot as plt
from ipywidgets import interact
def do_plot(x=0.2, y=0.9):
plt.text(x, y,'To move',size=19)
plt.show()
interact(do_plot)

Related

Edit a figure from external function

I'm trying to edit a figure that was obtained using an external function.
For example, "nolds.lyap_r" function creates a figure. Let's say I want to add a title to it after it was plotted. How can I do it?
import nolds
import numpy as np
import matplotlib.pyplot as plt
nolds.lyap_r(y, debug_plot=True)
I guess I need to use plt.gca() or plt.gcf() but it didn't work for me:
ax=plt.gca()
ax.set_title("ABC")
pyplot.draw() is there to update the existing figure:
plt.title('A good title')
plt.draw()

Need to run cell twice for the changed code to show output

I've run into an issue where I need to run the same cell twice after making a change. I've included a gif and the code.
In the gif I first change the seaborn style to darkgrid and run it, this should show the output as changed to the specified style on the first run, but I need to run it twice in order for the output to change.
Here is the code:
%matplotlib inline
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,14,100)
for i in range(1,5):
plt.plot(x,np.sin(x+i*0.5)*(7-i))
sns.set_style("white", {'axes.axisbelow': False})
plt.show()
I have tried separating the import lines to a previous cell but still the problem persists
set your style, before you plot anything . Move the line sns.set_style before for loop. It should work.
%matplotlib inline
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
sns.set_style("darkgrid", {'axes.axisbelow': False})
x = np.linspace(0,14,100)
for i in range(1,5):
plt.plot(x,np.sin(x+i*0.5)*(7-i))
plt.show()

How to change bars' outline width in a displot?

I managed to make a displot as I intended with seaborn and the only thing I want to change is the bars' outline width. Specifically, I want to make it thinner. Here's the code and a sample of how the dataframe is composed.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data_final = pd.merge(data, data_filt)
q = sns.displot(data=data_final[data_final['cond_state'] == True], y='Brand', hue='Style', multiple='stack')
plt.title('Sample of brands and their offering of ramen styles')
I'm specifying that the plot should only use rows where the cond_state is True. Here is a sample of the data_final dataframe.
Here is how the plot currently looks like.
I've tried various ways published online, but most of them use the deprecated distplot instead of displot. There also doesn't seem to be a parameter for changing the bars' outline width in the seaborn documentation for displot and FacetGrid
The documentation for the seaborn displot function doesn't have this parameter listed, but you can pass matplotlib axes arguments, such as linewidth = 0.25, to the seaborn.displot function to solve your problem.

How to create a box plot from a frequency table

In the table below, I have values and frequencies. I'd like to draw a box-plot using Jupyter Notebook. I googled it but not able to find any answers.
My idea is to create a column, 2,2,2,2,4,4,4,4,4,4,4,...
But I think there must be a better way.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
value=np.array([2,4,6,7,10])
freq=np.array([4,7,8,5,2])
# do something here
plt.boxplot(newdata)
plt.show()
use numpy's repeat:
newdata = np.repeat(value,freq)

Seaborn heatmap colors are reversed

I'm generating a heatmap from a pandas dataframe using a code that looks like this on my apple computer.
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(14,14))
sns.set(font_scale=1.4)
sns_plot = sns.heatmap(df, annot=True, linewidths=.5, fmt='g', ax=ax).set_yticklabels(ax.get_yticklabels(), rotation=0)
ax.set_ylabel('Product')
ax.set_xlabel('Manufacturer')
ax.xaxis.set_ticks_position('top')
ax.xaxis.set_label_position('top')
fig.savefig('output.png')
And I get a heatmap looking like this:
I then put my code in a docker container with an ubuntu image and I install the same version of seaborn. The only difference is that I need to add a matplotlib configuration so that TCL doesn't scream:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
And I get a heatmap that looks like this (I use the same code and the same pandas dataframe):
I'm unable to find why the color gradient is inverted and would love to hear if you have any idea.
Thank you !
The default colormap has changed to 'rocket' for sequential data with 0.8 release of seaborn, see the release notes. The colormap looks this way now:
You can always use the cmap argument and specify which colormap you prefer to use. For example, to get the pre-0.8 colormap for non-divergent data use: cmap=sns.cubehelix_palette(light=.95, as_cmap=True).