Use slider to choose which graph to show - python

Suppose I want to show three simulations by reading three .xlsx files.
Next, I want to design a slider to choose which simulation to show.
If I move the slider to 0, then 0 will be the input to the function "update()". The first simulation will be shown.
The following is the code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.widgets import Slider
import pandas as pd
import ipywidgets as wg
# input files
rm = pd.read_excel("test_3d.xlsx", header = None)
rm1 = pd.read_excel("test_3d1.xlsx", header = None)
rm2 = pd.read_excel("test_3d2.xlsx", header = None)
rec = np.shape(rm)
X = np.arange(1,rec[1]+1,1)
Y = np.arange(1,rec[0]+1,1)
x , y = np.meshgrid(X,Y)
# Set 3D plots
fig = plt.figure()
ax1 = fig.add_axes([0, 0, 1, 0.8], projection = '3d')
# Choose which 3D plots to show
def update(val):
if val == 0:
ax1.cla()
ax1.plot_surface(x, y, rm, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
elif val == 1:
ax1.cla()
ax1.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
elif val == 2:
ax1.cla()
ax1.plot_surface(x, y, rm2, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
ax1.set_zlim(-110, -80)
# Design a slider to choose which simulation to show
slider = wg.IntSlider(value=1, min=0, max=2, description='this is slider')
slideroutput = wg.Output()
display(slider, slideroutput)
numberonslider = []
def on_value_change(change):
with slideroutput:
numberonslider.append(change['new'])
print(numberonslider[-1])
ddd = slider.observe(on_value_change, names='value')
update(ddd)
If I move the slider, "ddd" gives you a list of 0, 1 or 2.
However, 3D-simulation does not show up. How to modify the code?

I'm using JupyterLab. I need %matplotlib widget for any kind of interactive matplotlib plot. The below code works fine, but won't work without %matplotlib widget.
warning: %matplotlib widget is not the same as import matplotlib.widget
You didn't provide any sample data, so I just made up some data. Basically the structure of your code was not correct, the if part should be inside the def on_value_change(change):. See the code below:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import ipywidgets as wg
%matplotlib widget
X = np.arange(5)
Y = np.arange(5)
x, y = np.meshgrid(X, Y)
rm = np.sin(x)
rm1 = np.cos(x)
rm2 = y
fig = plt.figure()
ax1 = fig.add_axes([0, 0, 1, 0.8], projection = '3d')
# Design a slider to choose which simulation to show
slider = wg.IntSlider(value=1, min=0, max=2, description='this is slider')
slideroutput = wg.Output()
display(slider, slideroutput)
ax1.cla()
plt.title(f"This is slider number {str(slider.value)}")
ax1.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
numberonslider = []
def on_value_change(change):
with slideroutput:
numberonslider.append(change['new'])
if numberonslider[-1] == 0:
ax1.cla()
plt.title(f"This is slider number {str(slider.value)}")
ax1.plot_surface(x, y, rm, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
elif numberonslider[-1] == 1:
ax1.cla()
plt.title(f"This is slider number {str(slider.value)}")
ax1.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
elif numberonslider[-1] == 2:
ax1.cla()
plt.title(f"This is slider number {str(slider.value)}")
ax1.plot_surface(x, y, rm2, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
slider.observe(on_value_change, names='value')
Output:
ask again if my explanation is somehow unclear.

Related

How to annotate and correctly place numbers in a heatmap

I'm having problems with heatmap.
I create the following function to show the analysis with heatmap
data = [ 0.00662896, -0.00213044, -0.00156812, 0.01450994, -0.00875174, -0.01561342, -0.00694762, 0.00476027, 0.00470659]
def plot_heatmap(pathOut, data, title, fileName, precis=2, show=False):
from matplotlib import cm
fig = plt.figure()
n = int(np.sqrt(len(data)))
data = data.reshape(n,n)
heatmap = plt.pcolor(data,cmap=cm.YlOrBr)
xLabels = (np.linspace(1,n,n,dtype=int))
yLabels = (np.linspace(1,n,n,dtype=int))
xpos = np.linspace(1,n,n)-0.5
ypos = np.linspace(1,n,n)-0.5
for y in range(n):
for x in range(n):
plt.text(x + 0.5, y + 0.5, f'{data[y, x]:.{precis}f}',
horizontalalignment='center',
verticalalignment='center',
)
plt.colorbar(heatmap, format='%.2f')
plt.xticks(xpos,xLabels)
plt.yticks(ypos,yLabels)
plt.title(f'{title}')
if (show == False ):
plt.close(fig)
elif (show == True):
plt.show()
fig.savefig(f'{pathOut}/{fileName}.pdf', format='pdf')
When I call the function the heatmap is created but not correctly, because I would like to show values at a specific precision. I know how to define text precision and scale precision, but how to adjust data precision to generate the correct heatmap?
In the attached figure, I have 7 cells equal to 0, for my desired precision, but the data used has a larger precision what produce different colors.
It is much easier to use seaborn.heatmap, which includes annotations and a colorbar. seaborn is a high-level API for matplotlib.
This significantly reduces the number of lines of code.
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import seaborn as sns
def plot_heatmap(pathOut, fileName, data, title, precis=2, show=False):
n = int(np.sqrt(len(data)))
data = data.reshape(n, n)
xy_labels = range(1, n+1)
fig, ax = plt.subplots(figsize=(8, 6))
p = sns.heatmap(data=data, annot=True, fmt=f'.{precis}g', ax=ax,
cmap=cm.YlOrBr, xticklabels=xy_labels, yticklabels=xy_labels)
ax.invert_yaxis() # invert the axis if desired
ax.set_title(f'{title}')
fig.savefig(f'{pathOut}/{fileName}.pdf', format='pdf')
if (show == False ):
plt.close(fig)
elif (show == True):
plt.show()
data = np.array([ 0.00662896, -0.00213044, -0.00156812, 0.01450994, -0.00875174, -0.01561342, -0.00694762, 0.00476027, 0.00470659])
plot_heatmap('.', 'test', data, 'test', 4, True)
The f-string for plt.txt is not correct. It will be easier to round the value and convert it to a str type.
str(round(data[x, y], precis)) instead of f'{data[y, x]:.{precis}f}'
data[x, y] should be data[y, x]
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
def plot_heatmap(pathOut, fileName, data, title, precis=2, show=False):
fig = plt.figure(figsize=(8, 6))
n = int(np.sqrt(len(data)))
data = data.reshape(n, n)
heatmap = plt.pcolor(data, cmap=cm.YlOrBr)
xLabels = (np.linspace(1,n,n,dtype=int))
yLabels = (np.linspace(1,n,n,dtype=int))
xpos = np.linspace(1,n,n)-0.5
ypos = np.linspace(1,n,n)-0.5
for y in range(n):
for x in range(n):
s = str(round(data[y, x], precis)) # added s for plt.txt and reverse x and y for data addressing
plt.text(x + 0.5, y + 0.5, s,
horizontalalignment='center',
verticalalignment='center',
)
plt.colorbar(heatmap, format=f'%.{precis}f') # add precis to the colorbar
plt.xticks(xpos,xLabels)
plt.yticks(ypos,yLabels)
plt.title(f'{title}')
fig.savefig(f'{pathOut}/{fileName}.pdf', format='pdf') # this should be before plt.show()
if (show == False ):
plt.close(fig)
elif (show == True):
plt.show()
# the function expects an array, not a list
data = np.array([ 0.00662896, -0.00213044, -0.00156812, 0.01450994, -0.00875174, -0.01561342, -0.00694762, 0.00476027, 0.00470659])
# function call
plot_heatmap('.', 'test', data, 'test', 4, True)

Interactive Stock Chart, step by step animation with a slider. Matplolib & Jupyter

In this post: Interactive Stock Chart, step by step animation with keyboard arrows, with Matplolib, I wrote a code, in which the user Zephyr brilliantly fixed, that interactively simulate a stock using keyboard arrows.
It turned out that I found a way of doing the same thing in Jupyter, using the module ipywidgets. The code works, but unfortunately the same chart is plotted twice. I have no idea why this is happening. Can someone help? I just want to show one plot (notice that the second plot does not move as I use the slider).
Here is the code:
%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('all_stocks_5yr.csv')
df_apple = df[df['Name'] == 'AAPL'].copy()
df_apple['date'] = pd.to_datetime(df_apple['date'])
df_apple.reset_index(inplace = True)
bars_to_display = 60
step = widgets.IntSlider(value=0, min=0, max=len(df_apple)-bars_to_display)
val_array = []
for idx, val in df_apple.iterrows():
val_array.append(val)
x = np.arange(0, len(df_apple))
fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)
def f(step):
ax.cla()
ax2.cla()
for i in range(step, bars_to_display + step):
color = '#2CA453'
if val_array[i]['open'] > val_array[i]['close']: color = '#F04730'
ax.plot([x[i], x[i]], [val_array[i]['low'], val_array[i]['high']], color = color)
ax.plot([x[i], x[i] - 0.1], [val_array[i]['open'], val_array[i]['open']], color = color)
ax.plot([x[i], x[i] + 0.1], [val_array[i]['close'], val_array[i]['close']], color = color)
ax2.bar(x[i], val_array[i]['volume'], color = 'lightgrey')
display(fig)
display(step)
out = widgets.interactive_output(f, {'step': step})
display(out)
The line:
fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)
draws the first figure. Just add plt.close() after that.
Complete Code
from IPython.display import display
from ipywidgets import interactive, widgets
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
df = pd.read_csv('all_stocks_5yr.csv')
df_apple = df[df['Name'] == 'AAPL'].copy()
df_apple['date'] = pd.to_datetime(df_apple['date'])
df_apple.reset_index(inplace = True)
bars_to_display = 60
step = widgets.IntSlider(value = 0, min = 0, max = len(df_apple) - bars_to_display)
val_array = []
for idx, val in df_apple.iterrows():
val_array.append(val)
x = np.arange(0, len(df_apple))
fig, (ax, ax2) = plt.subplots(2, figsize = (12, 8), gridspec_kw = {'height_ratios': [4, 1]}, sharex = True)
plt.close()
def f(step):
ax.cla()
ax2.cla()
for i in range(step, bars_to_display + step):
color = '#2CA453'
if val_array[i]['open'] > val_array[i]['close']: color = '#F04730'
ax.plot([x[i], x[i]], [val_array[i]['low'], val_array[i]['high']], color = color)
ax.plot([x[i], x[i] - 0.1], [val_array[i]['open'], val_array[i]['open']], color = color)
ax.plot([x[i], x[i] + 0.1], [val_array[i]['close'], val_array[i]['close']], color = color)
ax2.bar(x[i], val_array[i]['volume'], color = 'lightgrey')
display(fig)
display(step)
out = widgets.interactive_output(f, {'step': step})
display(out)

How to fix the title position of a plot after using color bar and slider

I have the following codes which use slider to choose which plots I want to show.
The input are three .xlsx files. The codes read the files and plot each of them by using slider to choose it.
Now I have the problem of fixing the position of the title of the plot.
The codes are shown below
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import ipywidgets as wg
import pandas as pd
import math
from matplotlib.ticker import LinearLocator
%matplotlib widget
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
rm = pd.read_excel("test_3d.xlsx", header = None)
rm1 = pd.read_excel("test_3d1.xlsx", header = None)
rm2 = pd.read_excel("test_3d2.xlsx", header = None)
rec = np.shape(rm)
rmm = np.concatenate([rm,rm1,rm2])
np.min(rmm)
X = np.arange(1,rec[1]+1,1)
Y = np.arange(1,rec[0]+1,1)
x , y = np.meshgrid(X,Y)
# Design a slider to choose which simulation to show
slider = wg.IntSlider(value=1, min=0, max=2, description='this is slider')
slideroutput = wg.Output()
display(slider, slideroutput)
# Initial plots
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
# After your choice
numberonslider = []
def on_value_change(change):
with slideroutput:
numberonslider.append(change['new'])
if numberonslider[-1] == 0:
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
elif numberonslider[-1] == 1:
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
elif numberonslider[-1] == 2:
ax.cla()
plt.title(f"Noise Result {str(slider.value)}")
surf = ax.plot_surface(x, y, rm2, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
ax.set_zlim(math.floor(np.min(rmm)), math.ceil(np.max(rmm)))
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter('{x:.02f}')
# Customize the z axis.
slider.observe(on_value_change, names='value')
# Add a color bar which maps values to colors.
cbaxes = fig.add_axes([0.87, 0.25, 0.01, 0.5])
cb = fig.colorbar(surf, cax = cbaxes)
cb.set_label('dBm', rotation=270)
plt.show()
My question is when I run the code (before move the slider), it shows
Now, after moving the slider, the title (Noise Result) of the plot moves to the top of the color bar.
How to fix this problem? Thanks!

Remove lowest color from colorbar in Seaborn/Matplotlib

If I set shade_lowest = False, the colorbar still contains the lowest level (purple-ish). Is there any generic way to remove it entirely?
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
a = np.random.normal(0, 1, 100)
b = np.random.normal(0, 1, 100)
fig, ax = plt.subplots()
sns.kdeplot(a, b, shade = True, shade_lowest = False, cmap = "viridis", cbar = True, n_levels = 4, ax = ax)
plt.show()
A solution is for sure to not create this level from the beginning.
Here we choose maximally 5 levels according to a locator and remove the lowest one when calling the contourf plot, such that this level does not even exist in the first place. Then the automatic colorbar creation works flawlessly.
import numpy as np; np.random.seed(5)
import matplotlib.pyplot as plt
from matplotlib import ticker
from scipy import stats
x = np.random.normal(3, 1, 100)
y = np.random.normal(0, 2, 100)
X, Y = np.mgrid[x.min():x.max():100j, y.min():y.max():100j]
positions = np.vstack([X.ravel(),Y.ravel()])
values = np.vstack([x,y])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
N=4
locator = ticker.MaxNLocator(N + 1, min_n_ticks=N)
lev = locator.tick_values(Z.min(), Z.max())
fig, ax = plt.subplots()
c = ax.contourf(X,Y,Z,levels=lev[1:])
ax.scatter(x,y, s=9, c="k")
fig.colorbar(c)
plt.show()

Matplotlib imshow/matshow display values on plot

I am trying to create a 10x10 grid using either imshow or matshow in Matplotlib. The function below takes a numpy array as input, and plots the grid. However, I'd like to have values from the array also displayed inside the cells defined by the grid. So far I could not find a proper way to do it. I can use plt.text to place things over the grid, but this requires coordinates of each cell, totally inconvenient. Is there a better way to do what I am trying to accomplish?
Thanks!
NOTE: The code below does not take the values from the array yet, I was just playing with plt.text.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
board = np.zeros((10, 10))
def visBoard(board):
cmap = colors.ListedColormap(['white', 'red'])
bounds=[0,0.5,1]
norm = colors.BoundaryNorm(bounds, cmap.N)
plt.figure(figsize=(4,4))
plt.matshow(board, cmap=cmap, norm=norm, interpolation='none', vmin=0, vmax=1)
plt.xticks(np.arange(0.5,10.5), [])
plt.yticks(np.arange(0.5,10.5), [])
plt.text(-0.1, 0.2, 'x')
plt.text(0.9, 0.2, 'o')
plt.text(1.9, 0.2, 'x')
plt.grid()
visBoard(board)
Output:
Can you do something like:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
min_val, max_val = 0, 10
ind_array = np.arange(min_val + 0.5, max_val + 0.5, 1.0)
x, y = np.meshgrid(ind_array, ind_array)
for i, (x_val, y_val) in enumerate(zip(x.flatten(), y.flatten())):
c = 'x' if i%2 else 'o'
ax.text(x_val, y_val, c, va='center', ha='center')
#alternatively, you could do something like
#for x_val, y_val in zip(x.flatten(), y.flatten()):
# c = 'x' if (x_val + y_val)%2 else 'o'
ax.set_xlim(min_val, max_val)
ax.set_ylim(min_val, max_val)
ax.set_xticks(np.arange(max_val))
ax.set_yticks(np.arange(max_val))
ax.grid()
Edit:
Here is an updated example with an imshow background.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
min_val, max_val, diff = 0., 10., 1.
#imshow portion
N_points = (max_val - min_val) / diff
imshow_data = np.random.rand(N_points, N_points)
ax.imshow(imshow_data, interpolation='nearest')
#text portion
ind_array = np.arange(min_val, max_val, diff)
x, y = np.meshgrid(ind_array, ind_array)
for x_val, y_val in zip(x.flatten(), y.flatten()):
c = 'x' if (x_val + y_val)%2 else 'o'
ax.text(x_val, y_val, c, va='center', ha='center')
#set tick marks for grid
ax.set_xticks(np.arange(min_val-diff/2, max_val-diff/2))
ax.set_yticks(np.arange(min_val-diff/2, max_val-diff/2))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlim(min_val-diff/2, max_val-diff/2)
ax.set_ylim(min_val-diff/2, max_val-diff/2)
ax.grid()
plt.show()
For your graph you should should try with pyplot.table:
import matplotlib.pyplot as plt
import numpy as np
board = np.zeros((10, 10))
board[0,0] = 1
board[0,1] = -1
board[0,2] = 1
def visBoard(board):
data = np.empty(board.shape,dtype=np.str)
data[:,:] = ' '
data[board==1.0] = 'X'
data[board==-1.0] = 'O'
plt.axis('off')
size = np.ones(board.shape[0])/board.shape[0]
plt.table(cellText=data,loc='center',colWidths=size,cellLoc='center',bbox=[0,0,1,1])
plt.show()
visBoard(board)
Some elaboration on the code of #wflynny making it into a function that takes any matrix no matter what size and plots its values.
import numpy as np
import matplotlib.pyplot as plt
cols = np.random.randint(low=1,high=30)
rows = np.random.randint(low=1,high=30)
X = np.random.rand(rows,cols)
def plotMat(X):
fig, ax = plt.subplots()
#imshow portion
ax.imshow(X, interpolation='nearest')
#text portion
diff = 1.
min_val = 0.
rows = X.shape[0]
cols = X.shape[1]
col_array = np.arange(min_val, cols, diff)
row_array = np.arange(min_val, rows, diff)
x, y = np.meshgrid(col_array, row_array)
for col_val, row_val in zip(x.flatten(), y.flatten()):
c = '+' if X[row_val.astype(int),col_val.astype(int)] < 0.5 else '-'
ax.text(col_val, row_val, c, va='center', ha='center')
#set tick marks for grid
ax.set_xticks(np.arange(min_val-diff/2, cols-diff/2))
ax.set_yticks(np.arange(min_val-diff/2, rows-diff/2))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlim(min_val-diff/2, cols-diff/2)
ax.set_ylim(min_val-diff/2, rows-diff/2)
ax.grid()
plt.show()
plotMat(X)

Categories

Resources