How to create a box plot from a frequency table - pandas

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)

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()

Can I see the graph in VS Code by using jupyter view?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("fivethirtyeight")
df = pd.DataFrame({'day':[1,2,3,4,5],'visitors':[200,302,480,590,680],'Bounce_rate':[20,30,40,50,60]})
df.set_index('day',inplace=True)
df.plot()
plt.show()
output is <Figure size 640x480 with 1 Axes>, the desired output is a graph (in VS Code).
I can see my code is correct when using cloud jupyter by achieving the desired output but it's not possible in VS Code jupyter view...
Am I doing something wrong? or is it something else?

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()

Seaborn y labels are overlapping

So I tried to make a categorical plot of my data and this is what my code and the graph.
import pandas as pd
import numpy as np
import matplotlib as plt
import seaborn as sns
sns.set(style="whitegrid")
sns.set_style("ticks")
sns.set_context("paper", font_scale=1, rc={"lines.linewidth": 6})
sns.catplot(y = "Region",x = "Interest by subregion",data = sample)
Image:
How can I make the y-labels more spread out and have a bigger font?
Try using sns.figure(figsize(x,y)) and sns.set_context(context=None,font_scale=1).
Try different values for these parameters to get the best results.

Use ipywidgets to interatively find best position matplotlib text

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)