Code Output Image
Desired Image
[
My CSV data consists of X axis value, Y axis value and Hardness value and I wanted to plot smooth heat map rather than in boxes like.
DATA:
import cv2
from skimage.io import imread, imshow
from skimage.transform import resize
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
from tqdm import tqdm
import pandas as pd
import seaborn as sns
from sklearn.neighbors import KernelDensity
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import style
from astropy.convolution import convolve, Gaussian2DKernel
from scipy.ndimage.filters import gaussian_filter
path = r"C:\Users\patels\Desktop\Ti/"
ids = os.listdir(path)
#print(ids)
for n, id_ in tqdm(enumerate(ids), total=len(ids)):
data = pd.read_excel(path+id_)
print(path+id_)
df1 = data[['HV 0.2', 'X pos. [mm]', 'Y pos. [mm]']]
heatmap1_data = pd.pivot_table(df1, values='HV 0.2', index=['Y pos. [mm]'], columns='X pos. [mm]')
plt.figure() #this creates a new figure on which your plot will appear
heatmap1 = sns.heatmap(heatmap1_data, cmap="viridis", vmin=300, vmax=400)
plt.title(ids[n]+'Ti Hardness Map')
Related
I create a mask of my dataset for plotting only No Animals materials, and when I draw this mask I have problems with the legends, because only the first material defines me and I don't know how to add the other 2 materials.
import numpy as np
import umap
import umap.plot
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import display,HTML
import cufflinks as cf
cf.set_config_file(sharing='public',theme='ggplot',offline=True)
import seaborn as sns
palpations = np.load('big_matrix_16384.npz',allow_pickle=True)
X = palpations['arr_0']
embedding = umap.UMAP(n_neighbors=50,
min_dist=0.2,
metric='correlation').fit(X)
emb = embedding.transform(X)
mask_1 = Data["Tipo"]=="Animal"
emb_tipo_1 = emb[mask_1]
cmap = plt.cm.Spectral
c =[sns.color_palette("Set2")[x] for x in data_tipo_1.Material.map({"bone":0, "cartilage":1, "liver_raw_piece1":2})]
plt.scatter(emb_tipo_1[:,0],
emb_tipo_1[:,1],
c=c,
label=np.unique(data_tipo_1.Material),s=10)
plt.gca().set_aspect("equal","datalim")
plt.title("UMAP muestras Animales.")
plt.legend()
enter image description here
I am trying to generate an aesthetically pleasing plot of the three species of iris in the dataset with the dots in the 2D embedding appropriately colored. I know of other ways to achieve that, but I would like to ask for help working out the wrinkles to get a nice plot with the examples colored appropriately using umap.plot(). Here is the code, which works if the line about the labels in the plot is left out (except that then there are no colors):
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
import seaborn as sns
import umap
import umap.plot
import holoviews
import datashader
from bokeh.plotting import show, save, output_notebook, output_file
from bokeh.resources import INLINE
dataset=load_iris()
data=pd.DataFrame(dataset["data"],columns=["Petal length","Petal Width","Sepal Length","Sepal Width"])
data["Species"]=dataset["target"]
data["Species"]=data["Species"].apply(lambda x: dataset["target_names"][x])
embedding = umap.UMAP(n_components=2, metric='hellinger').fit(data.iloc[:,[0,1,2,3]])
f = umap.plot.interactive(embedding,
hover_data=data.iloc[:,[4]],
labels= data.iloc[:,[4]],
point_size=10)
output_notebook(resources=INLINE)
show(f)
To facilitate things, this is the color-less (and useless) code:
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
import seaborn as sns
import umap
import umap.plot
import holoviews
import datashader
from bokeh.plotting import show, save, output_notebook, output_file
from bokeh.resources import INLINE
dataset=load_iris()
data=pd.DataFrame(dataset["data"],columns=["Petal length","Petal Width","Sepal Length","Sepal Width"])
data["Species"]=dataset["target"]
data["Species"]=data["Species"].apply(lambda x: dataset["target_names"][x])
embedding = umap.UMAP(n_components=2, metric='hellinger').fit(data.iloc[:,[0,1,2,3]])
f = umap.plot.interactive(embedding,
hover_data=data.iloc[:,[4]],
point_size=10)
output_notebook(resources=INLINE)
show(f)
And this is the different way to actually by-pass the entire data frame creation (which I really don't want to do), and go directly to the dataset to generate the plot - in my opinion it is still very ugly with some dots inconspicuous in light tan, and a huge rendition that forces to scroll through the window, but it is interactive:
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
import seaborn as sns
import umap
import umap.plot
import holoviews
import datashader
from bokeh.plotting import show, save, output_notebook, output_file
from bokeh.resources import INLINE
iris = load_iris()
embedding = umap.UMAP(n_components=2, metric='hellinger').fit(iris.data)
category_labels = pd.Series(iris.target).map(dict(zip(range(3),iris.target_names)))
hover_df = pd.DataFrame(category_labels, columns=['category'])
f = umap.plot.interactive(embedding, labels=category_labels,
hover_data=hover_df, point_size=10)
output_notebook(resources=INLINE)
show(f)
I am trying to plot 8000 points in three dimensions (x,y,z) of a terrain with matplotlib using the function contourf when I run the code I get the error
'OverflowError: In draw_path_collection: Exceeded cell block limit'
I tried to solve this with "mpl.rcParams['agg.path.chunksize'] = 20000"
but this did not resolve the issue. Here is my code
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from scipy.interpolate import griddata
import pandas as pd
import matplotlib as mpl
datos = pd.read_csv('zrh_terrain.txt', header =0)
dats=500
mpl.rcParams['agg.path.chunksize'] = 20000
X=datos.iloc[0:dats,0].values
Y=datos.iloc[0:dats,1].values
Z=datos.iloc[0:dats,2].values
dt_bar=np.linspace(Z.min(),Z.max(),10)
xi,yi= np.meshgrid(X,Y)
zi = griddata((X,Y),Z,(xi,yi),method='nearest')
plt.contourf(xi,yi,zi,extend='both',vmin=dt_bar[0],vmax=dt_bar[-1],
cmap=cm.terrain)
How does matplotlib ensure that a dataset can be within plot with specified size.
How do i from a plot stored as numpy, How do i read the color of the pixels illustration a datapoint (0,4) - in the plot.
example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")
First plot being :
Second plot being:
so the first has been resized to plot size 12,4 where the last basically plots the same data but just using the data shape as size... how do i change that?
Librosa just performs pcolormesh according to the GitHub source code
You need to define another figure with its own size for the second figure.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
fig = plt.figure(figsize=(12,4))
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
import matplotlib as mpl
import seaborn as sns
from scipy.stats import gaussian_kde
from numpy import linspace,hstack
LINE_WIDTH = 3
filename=('')
data=[ map(float, line.split()) for line in open(filename,'r') if line.strip()]
dataM=np.array(data)
meandata=np.mean(dataM,axis=0)
SD = np.std(dataM,axis=0)
sns.set_palette("hls")
mpl.rc("figure", figsize=(8, 4))
xs = np.linspace(meandata[0]-(4 * SD[0]) ,meandata[0]+( 4 * SD[0]), dataM[:,0].size)
ys=dataM[:,0]
n,bins,patches=plt.hist(ys,15)
I get this plot.
and I want to get a kernel gaussian distribution plotted over my histogram but I am getting an error TypeError: 'module' object is not callable
When I am trying to do this
my_pdf = gaussian_kde(ys)
x = linspace(30,100,1000)
plt(x,my_pdf(x),'r') # distribution function
plt.hist(ys,normed=1,alpha=.3) # histogram
plt.show()
What am I doing wrong?
You can do this directly using seaborn. It would be something like this:
import pandas as pd
import seaborn as sns
import scipy.stats
import matplotlib.pyplot as plt
data = pd.read_csv('input.txt')
sns.distplot(data, kde=False, fit=scipy.stats.norm)
plt.show()
For a kde plot just do:
sns.distplot(data);