This is the sequel to this article. com / kanedaq / items / 1e7a0e52363224c08980)
Histogram with matplotlib
--The vertical axis on the left is the frequency (default of matplotlib) --The vertical axis on the right is the relative frequency (total height of columns = 1)
And draw. However, it may be an evil way.
[python] How to draw a graph with two axes on the left and right with matplotlib
The number of data is 7,500, and it is a normal random number with an average value of 50 and a standard deviation of 10. First, try running the following Python code.
#%%
import numpy as np
import matplotlib.pyplot as plt
#%%
#Data creation
μ = 50
σ = 10
data = [ np.random.normal(μ, σ) for i in range(7500) ]
#%%
#Number of classes
num_bins = 20
#Graph drawing
fig = plt.figure(figsize=(12, 8))
plt.legend()
plt.xlabel('x', fontsize=18)
plt.title('null', fontsize=18)
# (1)Histogram with frequency on the vertical axis
ax1 = fig.add_subplot(111)
ax1.set_ylabel('frequency', fontsize=18)
ax1.grid(True, color="dimgray")
ax1.set_axisbelow(True) #Move grid to the back
ax1.hist(data, bins=num_bins, rwidth=0.5, align="mid")
# (2)Histogram with relative frequency on the vertical axis
ax2 = ax1.twinx()
ax2.set_ylabel('relative frequency', fontsize=18)
ax2.grid(True, color="lightgrey", linestyle="--")
ax2.set_axisbelow(True) #Move grid to the back
weights = np.ones_like(data) / len(data)
ax2.hist(data, bins=num_bins, weights=weights, rwidth=0.5, align="right", color="red")
The vertical axis = frequency column is drawn in the default color, and the vertical axis = relative frequency column is drawn in red, side by side. People confirm that the heights of both of them look the same </ font> (The reason why they are evil ...)
If the heights of the columns of frequency and relative frequency look the same, match the colors of the columns. Call ax2.hist () without specifying "color =" red "".
The Grid line of ax2 is displayed in front of the pillar of ax1. To make this horizontal line invisible, I decided to add the following code to overwrite the same graph as ax1.
# (3) (1)Adjust the appearance of
ax3 = ax1.twinx()
ax3.set_yticklabels([])
ax3.hist(data, bins=num_bins, rwidth=0.5, align="mid")
The entire code for the finished version is below.
#%%
import numpy as np
import matplotlib.pyplot as plt
#%%
#Data creation
μ = 50
σ = 10
data = [ np.random.normal(μ, σ) for i in range(7500) ]
#%%
#Number of classes
num_bins = 20
#Graph drawing
fig = plt.figure(figsize=(12, 8))
plt.legend()
plt.xlabel('x', fontsize=18)
plt.title('null', fontsize=18)
# (1)Histogram with frequency on the vertical axis
ax1 = fig.add_subplot(111)
ax1.set_ylabel('frequency', fontsize=18)
ax1.grid(True, color="dimgray")
ax1.set_axisbelow(True) #Move grid to the back
ax1.hist(data, bins=num_bins, rwidth=0.5, align="mid")
# (2)Histogram with relative frequency on the vertical axis
ax2 = ax1.twinx()
ax2.set_ylabel('relative frequency', fontsize=18)
ax2.grid(True, color="lightgrey", linestyle="--")
ax2.set_axisbelow(True) #Move grid to the back
weights = np.ones_like(data) / len(data)
ax2.hist(data, bins=num_bins, weights=weights, rwidth=0.5, align="right")
# (3) (1)Adjust the appearance of
ax3 = ax1.twinx()
ax3.set_yticklabels([])
ax3.hist(data, bins=num_bins, rwidth=0.5, align="mid")
It feels like an evil way, so please let me know if there is a straightforward approach.
Recommended Posts