Page 265 - Robotics and AI class 10
P. 265

13.  Plotting histograms - Use the top 200 YouTubers' dataset to plot and find out what genre was most liked.
                 Also plot followers using histogram for each genre.

              Ans.  import pandas as pd

                 import numpy as np
                 import matplotlib.pyplot as plt

                 # Generate imaginary data for the top 200 YouTubers' dataset
                 youtubers = 200

                 genres = ['Gaming', 'Travelling', 'Comedy', 'Education', 'Fitness']
                 likes = np.random.randint(100, 10000,youtubers)

                 followers = np.random.randint(100, 10000,youtubers)

                 genre = np.random.choice(genres,youtubers)
                 data = pd.DataFrame({'Genre': genre, 'Likes': likes, 'Followers': followers})

                 # Calculate the total likes for each genre
                 likes_by_genre = data.groupby('Genre')['Likes'].sum()

                 # Calculate the total followers for each genre
                 followers_by_genre = data.groupby('Genre')['Followers'].sum()

                 # Plotting the histogram for the total followers by genre

                 plt.figure(figsize=(10, 6))
                 plt.bar(followers_by_genre.index, followers_by_genre)

                 plt.xlabel('Genre')

                 plt.ylabel('Total Followers')
                 plt.title('Total Followers by Genre')

                 plt.xticks(rotation=45)
                 plt.tight_layout()

                 plt.show()
                 # Plotting the histogram for the total likes by genre

                 plt.figure(figsize=(10, 6))

                 plt.bar(likes_by_genre.index, likes_by_genre)
                 plt.xlabel('Genre')

                 plt.ylabel('Total Likes')
                 plt.title('Total Likes by Genre')

                 plt.xticks(rotation=45)
                 plt.tight_layout()





                                                                                            Assignment     263
   260   261   262   263   264   265   266   267   268   269   270