Bar charts and histograms are fundamental tools in data visualization, each serving unique purposes. A bar chart is used to display and compare the number, frequency, or other measures (e.g., mean) for different discrete categories of data. On the other hand, a histogram is used to summarize the distribution of a dataset by grouping data points into bins.
This example demonstrates how to create a simple bar chart using Matplotlib in Python. The bar chart visualizes the number of students enrolled in different courses.
import matplotlib.pyplot as plt
courses = ['Math', 'Science', 'English', 'History']
students = [50, 80, 70, 60]
plt.bar(courses, students, color='cyan')
plt.title('Number of Students in Each Course')
plt.xlabel('Courses')
plt.ylabel('Number of Students')
plt.show()
Here, we used the plt.bar()
function to create the bar chart. The courses
list contains the categories, while the students
list contains the corresponding values. The bars are colored cyan for better visibility.
A grouped bar chart is useful for comparing multiple series of data across the same categories. This example compares the number of male and female students in each course.
import numpy as np
import matplotlib.pyplot as plt
courses = ['Math', 'Science', 'English', 'History']
male_students = [20, 40, 35, 30]
female_students = [30, 40, 35, 30]
x = np.arange(len(courses))
width = 0.35
fig, ax = plt.subplots()
bars1 = ax.bar(x - width/2, male_students, width, label='Male', color='teal')
bars2 = ax.bar(x + width/2, female_students, width, label='Female', color='orange')
ax.set_xlabel('Courses')
ax.set_ylabel('Number of Students')
ax.set_title('Number of Male and Female Students in Each Course')
ax.set_xticks(x)
ax.set_xticklabels(courses)
ax.legend()
plt.show()
In this example, we use NumPy to manage the positions of the bars. The bars are grouped by shifting their positions slightly using the width variable.
Histograms are used to plot the frequency distribution of a dataset. This example illustrates a basic histogram of student scores.
import matplotlib.pyplot as plt
scores = [55, 70, 65, 80, 75, 60, 85, 90, 95, 100, 45, 50, 55, 60, 65]
plt.hist(scores, bins=5, color='red', alpha=0.7)
plt.title('Distribution of Student Scores')
plt.xlabel('Scores')
plt.ylabel('Frequency')
plt.show()
The plt.hist()
function is used to create the histogram. The bins
parameter specifies the number of intervals the data should be divided into. The histogram provides a visual representation of the distribution of scores.
A cumulative histogram shows the cumulative frequency of the data points. This example demonstrates how to create a cumulative histogram of student scores.
import matplotlib.pyplot as plt
scores = [55, 70, 65, 80, 75, 60, 85, 90, 95, 100, 45, 50, 55, 60, 65]
plt.hist(scores, bins=5, cumulative=True, color='blue', alpha=0.7)
plt.title('Cumulative Distribution of Student Scores')
plt.xlabel('Scores')
plt.ylabel('Cumulative Frequency')
plt.show()
The cumulative histogram is created by setting the cumulative
parameter to True
. It helps in understanding how the frequency of scores accumulates over intervals.
A stacked bar chart is useful for showing the composition of different groups. This example shows the total number of male and female students in each course.
import matplotlib.pyplot as plt
courses = ['Math', 'Science', 'English', 'History']
male_students = [20, 40, 35, 30]
female_students = [30, 40, 35, 30]
plt.bar(courses, male_students, color='teal', label='Male')
plt.bar(courses, female_students, bottom=male_students, color='orange', label='Female')
plt.xlabel('Courses')
plt.ylabel('Number of Students')
plt.title('Total Number of Students in Each Course')
plt.legend()
plt.show()
In a stacked bar chart, the bottom
parameter is used to stack the bars. This helps to visualize the total and the contribution of each group within the total.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies