# import pandas.rpy.common as com # # load the R package ISLR # infert = com.importr("ISLR") # # load the Auto dataset # auto_df = com.load_data('Auto') import pandas as pd import seaborn as sns %matplotlib inline data = { "x": range(0, 100), "y": range(100, 0, -1), "z": range(0, 100), "a": [4, 5] * 50, "b": [4, 5, 6, 7] * 25 } df = pd.DataFrame(data) print(df.head()) corr = df.corr() print(corr) # plot the heatmap sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns) OUTPUT IN JUPYTER NOTEBOOK: Changing color scheme of heatmap: Code: sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap="YlGnBu") Output: Another: cmap = sns.diverging_palette(5, 250, as_cmap=True) sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap = cmap) Viewing heatmap like color scheme in the Pandas textual output itself: Code: cmap = sns.diverging_palette(5, 250, as_cmap=True) def magnify(): rtnVal = [dict(selector="th", props=[("font-size", "10px")]), dict(selector="td", props=[('padding', "2px 2px")]), dict(selector="th:hover", props=[("font-size", "16px")]), dict(selector="tr:hover td:hover", props=[('max-width', '200px'), ('font-size', '16px')])] return [] corr.style.background_gradient(cmap, axis=1)\ .set_properties(**{'max-width': '80px', 'font-size': '14px'})\ .set_caption("Hover to magify")\ .set_precision(2)\ .set_table_styles(magnify()) Output:
Creating Heatmap from Pandas DataFrame correlation matrix
Subscribe to:
Posts (Atom)
No comments:
Post a Comment