Skip to content

Instantly share code, notes, and snippets.

@bluepine
Created November 10, 2018 01:42
Show Gist options
  • Save bluepine/1c8fd6fbc13d6c79ee603bddfcadd02c to your computer and use it in GitHub Desktop.
Save bluepine/1c8fd6fbc13d6c79ee603bddfcadd02c to your computer and use it in GitHub Desktop.
some data plotting functions
# pyline: disable=no-member
""" plot3d using existing visuals : LinePlotVisual """
import numpy as np
import sys
import seaborn as sns
from vispy import app, visuals, scene
import matplotlib.pyplot as plt
def line_plot3d_matplotlib(x, y, z):
plt.ioff()
# fig = plt.figure()
ax = plt.gca(projection='3d')
ax.set_xlabel('$X$', fontsize=20)
ax.set_ylabel('$Y$', fontsize=20)
ax.set_zlabel('$Z$', fontsize=20)
ax.plot(x, y, z, 'gray')
ax.scatter(x, y, z, 'gray')
# ax.legend()
plt.show()
def line_plot3d_vispy(x, y, z):
z = z.astype(float)
xm = np.mean(x)
ym = np.mean(y)
zm = np.mean(z)
x -= xm
y -= ym
z -= zm
x *= 1.0/max(x)
y *= 1.0/max(y)
z *= 1.0/max(z)
center = (0, 0, 0)
# build visuals
Plot3D = scene.visuals.create_visual_node(visuals.LinePlotVisual)
# build canvas
canvas = scene.SceneCanvas(keys='interactive', title='plot3d', show=True)
# Add a ViewBox to let the user zoom/rotate
view = canvas.central_widget.add_view()
view.camera = 'turntable'
view.camera.fov = 45
view.camera.distance = 2
view.camera.center = center
# plot
pos = np.c_[x, y, z]
Plot3D(pos, width=2.0, color='red',
edge_color='w', symbol='o', face_color=(0.2, 0.2, 1, 0.8),
parent=view.scene)
app.run()
def hist(data_frame):
for column in data_frame:
if 'id' != column and data_frame[column].dtype == 'object':
sns.catplot(x=column, kind="count", palette="ch:.25",
data=data_frame, log=True)
data_frame.hist(bins=15, color='steelblue', edgecolor='black',
linewidth=1.0, xlabelsize=8, ylabelsize=8, grid=False, log=True)
plt.show()
def corr_heatmap(data_frame):
"""Correlation Matrix Heatmap"""
# f, ax = plt.subplots(figsize=(10, 6))
corr = data_frame.corr()
sns.heatmap(round(corr, 2), annot=True, cmap="coolwarm", fmt='.2f',
linewidths=.05)
plt.show()
# f.subplots_adjust(top=0.93)
# t = f.suptitle('Wine Attributes Correlation Heatmap', fontsize=14)
def select_cols_by_bool_vec(bv, data_frame):
cols = bv.index[bv]
return data_frame[cols]
def pairplot(data_frame, corr_threshold=0.5):
corr = data_frame.corr()
np.fill_diagonal(corr.values, 0)
corr_index = corr.max().abs() > corr_threshold
sns.pairplot(select_cols_by_bool_vec(corr_index, data_frame).dropna())
if __name__ == '__main__':
# prepare data
N = 6000
x = np.sin(np.linspace(-2, 2, N)*np.pi)
y = np.cos(np.linspace(-2, 2, N)*np.pi)
z = np.linspace(-2, 2, N)
if sys.flags.interactive != 1:
line_plot3d_vispy(x, y, z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment