Page 159 - Touhpad Ai
P. 159

Example: Let us consider the average temperature in Delhi and then draw a line graph from the available data:

                      Month        Jan     Feb    Mar    Apr    May     Jun    Jul    Aug    Sep     Oct    Nov    Dec

                   Temperature      10     15     25      37     45     47     46      40     38     30     25      16


























                    Program 1: Create a Line Graph for different temperature in different months using Python.


                      Month         Jan    Feb    Mar     Apr    May     Jun    Jul    Aug     Sep    Oct    Nov    Dec

                   Temperature       10     15     25      37     45     47      46     40     38     30      25     16

                 Code:

                 import matplotlib.pyplot as plt

                 # Sample data for the line graph
                 months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                 temperatures = [10, 15, 25, 37, 45, 47, 46, 40, 38, 30, 25, 16]


                 ' ' ' Create a line graph using plot() function with customized markers, linewidth, and
                 linestyle' ' '
                 plt.plot(months, temperatures,
                          marker='o', markersize=8, markeredgecolor='red',
                          linestyle='--', linewidth=2, color='b', label='Temperature')

                 # Add titles and labels
                 plt.title('Average Monthly Temperatures in Delhi')
                 plt.xlabel('Month')
                 plt.ylabel('Temperature (°C)')

                 # Show the legend
                 plt.legend()


                 # Display the plot
                 plt.show()




                                                                                                  Data Visualization  157
   154   155   156   157   158   159   160   161   162   163   164