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

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?

Related

Unable to generate plot using matplotlib

I am a beginner to Python and experimenting with a plot. the script runs fine but plot does not show up.
the matplotlib and numpy libraries are installed.
import numpy as np
f= h5py.File('3DIMG_05JUN2021_0000_L3B_HEM_DLY.h5','r')
#Studying the structure of the file by printing what HDF5 groups are present
for key in f.keys():
print(key) #Names of the groups in HDF5 file.
# will print the variables in the file
#Get the HDF5 group
ls=list(f.keys())
print("ls")
print(ls)
tsurf = f['HEM_DLY'][:]
print("tsurf")
print(tsurf)
tsurf1=np.squeeze(tsurf)
print(tsurf1.shape)
import matplotlib.pyplot as plt
im= plt.plot(tsurf1)
#plt.colorbar()
plt.imshow(im)```
Python version is 3 running on Ubuntu
Difficult to give you the exact answer without the dataset (please update the question with the dataset), but for sure, plt.plot does not return an object that can be plotted with plt.imshow
Try instead:
ax = plt.plot(tsurf1)
plt.show()
Probably the error was on the final plot.Try this:
import numpy as np
import matplotlib.pyplot as plt
f= h5py.File('/path','r')
ls=list(f.keys())
tsurf = f['your_key_str'][:]
tsurf1=np.squeeze(tsurf)
im= plt.plot(tsurf1)
plt.show(im) # <-- plt.show() NOT plt.imshow()

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

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)