matplotlib code does not shows anything on output - matplotlib

I am following this here: https://matplotlib.org/users/image_tutorial.html
The code is this:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread('1.jpg')
I am trying to output something, but when I execute the code, I get nothing... Shouldn't I get a matrix as output?

Related

How to show the peaks of pmf by matplotlib and scipy?

this is the code(I want to know the peak of the picture but I don't know how to add this kind of code)
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from scipy import stats
n=25
p=0.6
k=np.arange(0,50)
#the pmf forming
picture=stats.binom.pmf(k,n,p)
print(picture)
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
mean,var,skew,kurt=stats.binom.stats(n,p,moments='mvsk')
print(mean,var,skew,kurt)
#the picture forming
plt.plot(k,picture,'o-')
plt.grid(True)
plt.show()
You can use scatter
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from scipy import stats
n=25
p=0.6
k=np.arange(0,50)
#the pmf forming
picture=stats.binom.pmf(k,n,p)
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
mean,var,skew,kurt=stats.binom.stats(n,p,moments='mvsk')
print(mean,var,skew,kurt)
#the picture forming
plt.plot(k,picture,'o-')
plt.grid(True)
# the two new lines
max_ind = np.argmax(picture)
plt.scatter(x=k[max_ind],y=picture[max_ind],c='r',s=100,zorder=10)
and this produces

Why df.interpotale() doesn't work in my python3? why does it return the exact same dataframe withut any interpolation?

my code for df.interpolate was:
import pandas as pd
import numpy as np
import xlrd
from IPython.display import display
from scipy import interpolate
pd.set_option('display.max_rows',54100)
df = pd.read_excel(r'C:\Users\User\Desktop\tanvir random practice\gazipur.xlsx', parse_date=["DateTime"], index_col='DateTime']
df.interpolate(method="linear").bfill()
display(df)

Why histogram ticks showing different answers in gui in compare with non-gui?

I am using jupyter notebook and I want to draw histograms. When I do not use GUI it is okay and everything is shown correctly but when I use the Tkinter version of the code, all bars in histogram are shifted to left so the first bar is missing.(e.g: It should be 4 on a,3 on b,9 on c but it shows 3 on a,9 on b, where a,b and c are ticks)
this is the first code i do not use gui:
import Tkinter as tk
from Tkinter import*
import tkMessageBox
import tkFileDialog
import pandas as pd
import pyautogui
import os
from PIL import Image, ImageTk
from tkinter import ttk
import pylab as plt
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
select1=pd.DataFrame()
select2=pd.DataFrame()
year1=1396
year2=1395
select1=df.loc[(df['yearj']==year1)]
select2=df.loc[(df['yearj']==year2)]
x=select1['monthj'].values.tolist()
y=select2['monthj'].values.tolist()
plt.xlabel('month')
plt.ylabel('number of orders')
bins=[1,2,3,4,5,6,7,8,9,10,11,12,13]
axx=plt.subplot()
axx.xaxis.set_major_locator(MultipleLocator(1))
axx.xaxis.set_major_formatter(FormatStrFormatter('%d'))
plt.hist(y,bins,rwidth=0.8)
plt.hist(x,bins,rwidth=0.8, alpha=0.6)
and the output is:
enter image description here
and here is second code:
import pandas as pd
import numpy
import pylab as plt
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
def compare_months(df,hh):
compmon = tk.Toplevel(h
bins=[1,2,3,4,5,6,7,8,9,10,11,12,13]
select1=pd.DataFrame()
select2=pd.DataFrame()
year1=1396
year2=1395
select1=df.loc[(df['yearj']==year1)]
select2=df.loc[(df['yearj']==year2)]
xr=select1['monthj'].values.tolist()
yr=select2['monthj'].values.tolist()
xr.sort(key=int)
yr.sort(key=int)
f = Figure(figsize=(7,6), dpi=80)
f.add_axes([0.15, 0.15,0.8,0.7])
canvas = FigureCanvasTkAgg(f, master=compmon)
canvas.get_tk_widget().grid(row=4, column=5, rowspan=8)
p = f.gca()
p.set_xlabel('month', fontsize = 10)
p.set_ylabel('number of orders', fontsize = 10)
p.hist(yr,bins,rwidth=0.8)
p.hist(xr,bins,rwidth=0.8, alpha=0.6)
p.xaxis.set_major_formatter(FormatStrFormatter('%d'))
p.xaxis.set_major_locator(MultipleLocator(1))
but=Button(compmon, text="ok", command=compare_months(df,root))
but.grid(row=2,column=2)
and the output is:enter image description here
Why does this happen?

Wordcloud using matplotlib is not showing

For my code, please see below:
#tfids words word cloud
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import pandas as pd
tf = pd.DataFrame(columns=['word'])
tf['word'] = ['federalist', 'wrexham', 'remy', 'islamic', 'hegseth', 'hereford', 'gbt', 'sharenet', 'cpr', 'vegas', 'krvn', 'bandidos', 'nginx', 'manafort' , 'roth', 'kennedy' ,'pence', 'quantum']
wordcloud10 = WordCloud().generate(' '.join(tf['word']))
plt.imshow(wordcloud10)
plt.axis("off")
plt.title("")
plt.show()
display()
In databricks, one has to use display to see a chart. Despite the above code, I don't see a wordcloud. The O/p i see as below:
Out[1106]: <matplotlib.image.AxesImage at 0x7fae917806d0>
Out[1106]: (-0.5, 399.5, 199.5, -0.5)
Out[1106]: <matplotlib.text.Text at 0x7faeb31a6110>
Thanks for your help.
Screenshot from Databricks - No Chart

Add thousands comma separator to Seaborn Heatmap [duplicate]

I am trying to format my colorbar such the numbers are formatted with commas. Any help would be greatly appreciated
import numpy as np
import matplotlib.pyplot as plt
plt.matshow(np.array(([30000,8000],[12000,25000])))
plt.colorbar()
You can create and specify a custom formatter:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
comma_fmt = FuncFormatter(lambda x, p: format(int(x), ','))
plt.matshow(np.array(([30000,8000],[12000,25000])))
plt.colorbar(format=comma_fmt)
plt.show()