BTech_5_CSE_Data_Analytics_Using_Python_Unit_5_Notes
BTech_5_CSE_Data_Analytics_Using_Python_Unit_5_Notes
UNIT – 5
Visualization with Matplotlib
This chart allows you to plot values and provides a starting point for adding more
interactive elements.
SRGI, BHILAI
Explanation:
• color='green': Sets the line color.
• linestyle='--': Makes the line dashed.
• marker='o': Adds markers at each data point.
• markersize=8: Sets marker size.
• linewidth=2: Sets line thickness.
• fontsize and color adjust the axis and title fonts.
plt.ylabel("sin(X)")
plt.legend()
plt.show()
Explanation:
• np.linspace(0, 10, 100): Creates an array of 100 points between 0 and 10.
• np.sin(x): Applies the sine function to each value in x.
5. Adding Text
Adding text annotations helps highlight key points in the data.
plt.plot(x, y, 'g')
plt.title("Annotated Sinusoidal Chart")
plt.text(5, 0, "Middle Point", fontsize=12, color='purple') # Add text at
coordinates (5,0)
plt.show()
SRGI, BHILAI
Explanation:
• plt.text(x, y, "text"): Adds a text label at the specified (x, y) coordinates.
6. Adding a Grid
Adding a grid improves readability, especially for larger datasets.
plt.plot(x, y, 'b')
plt.title("Chart with Grid")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True) # Add grid to the chart
plt.show()
7. Adding a Legend
A legend identifies different series on a chart, useful when plotting multiple lines.
plt.plot(x, y, label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)", linestyle='--')
plt.title("Sine and Cosine Waves")
SRGI, BHILAI
plt.xlabel("X values")
plt.ylabel("Y values")
plt.legend(loc="upper right") # Position the legend
plt.show()
Explanation:
• label="text" assigns a label to each line.
• plt.legend(loc="upper right") places the legend at the top right.
Explanation:
• dpi=300 specifies the resolution.
• format='png' saves as a PNG file.
SRGI, BHILAI
Histogram
Histograms are used to show the distribution of a dataset.
data = np.random.randn(1000) # Random data
plt.hist(data, bins=30, color='green', alpha=0.7)
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
Explanation:
• bins=30: Divides the data range into 30 intervals.
• alpha=0.7: Sets transparency.
SRGI, BHILAI
Bar Chart
Bar charts are used to compare categories.
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 10]
plt.bar(categories, values, color='purple')
plt.title("Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
Pie Chart
Pie charts are useful for showing the proportion of categories in a dataset.
sizes = [15, 30, 45, 10]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
plt.title("Pie Chart")
plt.show()
SRGI, BHILAI
Explanation:
• autopct='%1.1f%%': Displays percentage values on the chart.
• startangle=140: Rotates the chart for a better view.
Summary
This guide covers:
1. Line Charts for trends.
2. Histograms for distributions.
3. Bar Charts for category comparisons.
4. Pie Charts for proportions.
5. Customization Options such as grids, legends, annotations, and multiple
figures.
matplotlib is a powerful library, enabling rich, customizable visualizations. The
above examples should provide a foundation for creating effective data
visualizations in Python.