Python Matplotlib Questions
Python Matplotlib Questions
Task: Write the Matplotlib code to plot both cities on the same line chart.
● Use a dashed line with circle markers for City A, and a solid line with square markers for
City B.
● Include a legend indicating which line belongs to which city.
● Add appropriate labels for the x-axis, y-axis, and a main title.
Q2: Scatter Plot (Multivariate Data) A climate researcher wants to visualize the
relationship between the Air Quality Index (AQI) and the number of reported respiratory
clinic visits, while also factoring in local humidity.
Task: Write the code to generate a Scatter Plot mapping aqi to the x-axis and
clinic_visits to the y-axis.
● Use the humidity array to determine the color of the data points (using a colormap like
"viridis").
● Add a colorbar to the side of the plot to indicate the humidity scale.
● Set the transparency (alpha) of the points to 0.7.
Q3: Horizontal Bar Chart (Ranking Data) An environmental agency needs to present
the top 5 countries by renewable energy capacity (in Gigawatts).
● countries = [Link](["China", "USA", "Brazil", "India",
"Germany"])
● capacity_gw = [Link]([1100, 350, 180, 160, 140])
Task: Write the Matplotlib code to create a horizontal Bar Chart (barh).
Q4: Pie Chart (Highlighting Proportions) You need to illustrate a city's power grid
energy mix to highlight its reliance on fossil fuels versus renewables.
● Explode (pull out) the "Fossil Fuels" slice by 0.1 to emphasize it.
● Display the percentage values on the slices formatted to one decimal place (e.g.,
15.0%).
● Add a shadow effect to the pie chart for depth.
Q5: Histogram (Data Distribution) You have a large NumPy array representing the
daily rainfall (in mm) recorded over 365 days in a tropical region.
Task: Write the Matplotlib code to plot a Histogram showing the frequency distribution of the
rainfall data.