Folder:
112 Data Visualizations
File:
112.10 Data visualization with python

Data visualization with python

These notes were cut short because I found it much more valuable to do the visualizations than write about them. See Redata, Blog, for some examples.

Graph size

To make a bigger graph, set this param
plt.rcParams["figure.figsize"] = (20, 10)

And, to set it back to the default
plt.style.use("default")

  • https://www.mikulskibartosz.name/how-to-change-plot-size-in-jupyter-notebook/
  • https://stackoverflow.com/questions/26413185/how-to-recover-matplotlib-defaults-after-setting-stylesheet

To create a matrix plot of 1's and 0's

import pandas as pd
import numpy as np

# first make a dataframe with random 1's and 0's
df = pd.DataFrame(np.random.randint(0, 2, size=(100,4)), columns=list('1234'))

# then plot it
plt.imshow(df, cmap='GnBu')
plt.show()

This code also adds the grid lines in-between the dots

im = plt.imshow(df, cmap='GnBu')
ax = plt.gca()
ax.set_xticks(np.arange(-0.5, len(df.columns), 1), minor=True)
ax.set_yticks(np.arange(-0.5, len(df.index), 1), minor=True)
ax.grid(which='minor', color='w', linestyle='-', linewidth=2)

References:
- https://stackoverflow.com/questions/42116671/how-to-plot-a-2d-matrix-in-python-with-colorbar-like-imagesc-in-matlab
- https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.axes.Axes.imshow.html#matplotlib.axes.Axes.imshow
- https://www.thetopsites.net/article/54023008.shtml


Source:
  • Me