Learn matplotlib drawing from scratch (4): Parallel histogram

Keywords: Big Data

Accumulated histograms have the advantage of accumulating histograms. For example, we can easily see the trend of multi-classification summation.

However, we find that in the histogram, we can not easily understand the trend of the data classified above because of the different base positions.

Therefore, when classification is not particularly large, and we do not attach more importance to the total trend than the classification, we can consider the use of side-by-side histograms, which is also a very common graphics.

As before, we use Xiao Ming's scores in three subjects besides language number in 20 monthly examinations to demonstrate the parallel bar chart.

import matplotlib.pyplot as plt
import numpy as np

# Generate data
x = np.linspace(1, 21, 20)
y1 = np.random.randint(70, 90, 20)  # grade scores of Chinese
y2 = np.random.randint(80, 100, 20) # Mathematics achievement
y3 = np.random.randint(60, 80, 20)  # English achievement

# Drawing
plt.figure(figsize=(20, 10))
# Chinese
plt.bar(x, y1,
        width = 0.25,
        color = 'c',
        align = 'center',
        label = 'Chinese',
        alpha = 0.5
       )
# Mathematics
plt.bar(x+0.25, y2,
        width = 0.25,
        color = 'r',
        align = 'center',
        label = 'Mathematics',
        alpha = 0.5
       )
# English?
plt.bar(x+0.5, y3,
        width = 0.25,
        color = 'b',
        align = 'center',
        label = 'English?',
        alpha = 0.5
       )

# Adding Mean Reference Lines for Achievements in Three Subjects
# Chinese
plt.axhline(y = np.mean(y1),
           c = 'c',
           ls = '--',
           lw = 2, 
           alpha = 0.6)
# Mathematics
plt.axhline(y = np.mean(y2),
           c = 'r',
           ls = '--',
           lw = 2, 
           alpha = 0.6)
# English?
plt.axhline(y = np.mean(y3),
           c = 'b',
           ls = '--',
           lw = 2, 
           alpha = 0.6)
# pass line
plt.axhline(y = 60,
           c = 'gray',
           ls = '--',
           lw = 2, 
           alpha = 0.6)


# Legend
plt.legend(loc = 'upper right')

# Title
plt.title('Trend Map of Xiaoming's 20 Monthly Examinations')

It can be seen that Xiao Ming has some partial majors. His math scores are obviously better than those of the other two subjects. At the same time, his English scores are basically the worst. I'm afraid that when I get to college, Xiao Ming is a standard man of science and technology.

Here we use the axhline() method mentioned in the first section to add reference lines, so that we can clearly know whether Xiao Ming is higher or lower than the average for different subjects in each examination.

Posted by nareshrevoori on Thu, 31 Jan 2019 02:48:16 -0800