Grafik batang || histogram di python || matplotlib
grafik batang import matplotlib.pyplot as plt import numpy as np # make data: np.random.seed(3) x = 0.5 + np.arange(8) y = np.random.uniform(2, 7, len(x)) # plot fig, ax = plt.subplots() ax.bar(x, y, width=1, edgecolor="white", linewidth=0.7) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) plt.show() histogram import matplotlib.pyplot as plt import numpy as np # make data np.random.seed(1) x = 4 + np.random.normal(0, 1.5, 200) # plot: fig, ax = plt.subplots() ax.hist(x, bins=8, linewidth=0.5, edgecolor="white") ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 56), yticks=np.linspace(0, 56, 9)) plt.show() histogram2D import matplotlib.pyplot as plt import numpy as np # make data: correlated + noise np.random.seed(1) x = np.random.randn(5000) y = 1.2 * x + np.random.randn(5000) / 3 # plot: fig, ax = plt.subplots() ax.hist2d(x, y, bins=(np.arange(-3, 3, 0.1), np.arange(-3, 3, 0.1))) ax.set(xlim=(-2, 2), ylim=(-3, 3)) plt.show()
grafik batang import matplotlib.pyplot as plt import numpy as np # make data: np.random.seed(3) x = 0.5 + np.arange(8) y = np.random.uniform(2, 7, len(x)) # plot fig, ax = plt.subplots() ax.bar(x, y, width=1, edgecolor="white", linewidth=0.7) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) plt.show() histogram import matplotlib.pyplot as plt import numpy as np # make data np.random.seed(1) x = 4 + np.random.normal(0, 1.5, 200) # plot: fig, ax = plt.subplots() ax.hist(x, bins=8, linewidth=0.5, edgecolor="white") ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 56), yticks=np.linspace(0, 56, 9)) plt.show() histogram2D import matplotlib.pyplot as plt import numpy as np # make data: correlated + noise np.random.seed(1) x = np.random.randn(5000) y = 1.2 * x + np.random.randn(5000) / 3 # plot: fig, ax = plt.subplots() ax.hist2d(x, y, bins=(np.arange(-3, 3, 0.1), np.arange(-3, 3, 0.1))) ax.set(xlim=(-2, 2), ylim=(-3, 3)) plt.show()