Matplotlib Notes
Matplotlib Notes
================
What is Matplotlib?
- A comprehensive library for creating static, animated, and interactive visualizations in Python.
Basic Components
- Axes: A part of the figure where data is plotted (can have multiple).
Installation
Importing
x = [1, 2, 3, 4]
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Types of Plots
Line: plt.plot()
Scatter: plt.scatter()
Bar: plt.bar()
Histogram: plt.hist()
Customization Options
plt.xlabel("X Label")
plt.ylabel("Y Label")
plt.title("Title")
plt.legend(["Series 1"])
Subplots
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.subplot(1, 2, 2)
plt.bar(x, y)
plt.show()
Save Plot
plt.savefig("plot.png")
import pandas as pd
data = pd.read_csv("data.csv")
data.plot(kind='line')
plt.show()
Tips
======================================
Sample Data
-----------
df = pd.DataFrame(data)
1. Line Plot
2. Multiple Lines
3. Bar Chart
4. Grouped Bar
5. Stacked Bar
6. Histogram
df['Profit'].plot(kind='hist')
7. Box Plot
df[['Sales', 'Profit']].plot(kind='box')
8. Scatter Plot
9. Pie Chart
df.set_index('Year')['Sales'].plot(kind='pie')
Subplots
Annotations
=========================
x = [1, 2, 3, 4]
plt.scatter(x, y)
=============================
plt.xticks([1,2,3,4,5])
plt.yticks(range(10, 35, 5))
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(5))
plt.xlim(0, 6)
plt.ylim(5, 35)
plt.xticks(rotation=45)