Matplotlib Pandas Guide (1)
Matplotlib Pandas Guide (1)
plt.figure(figsize=(10, 6))
plt.plot(df['Date'], df['Price'], marker='o')
plt.title('Stock Price Trend 2023')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.grid(True)
plt.show()
Output:
plt.figure(figsize=(8, 6))
df.plot(kind='bar', x='Month', y='Sales')
plt.title('Monthly Sales Performance')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.show()
Output:
plt.figure(figsize=(8, 6))
plt.scatter(df['Height'], df['Weight'])
plt.title('Height vs Weight Correlation')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()
Scatter plot showing correlation
between height and weight
Output:
plt.figure(figsize=(8, 6))
df['Age'].hist(bins=30, edgecolor='black')
plt.title('Age Distribution')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()
Output:
plt.figure(figsize=(8, 6))
df.boxplot()
plt.title('Product Price Distribution')
plt.ylabel('Price ($)')
plt.show()
Output:
plt.figure(figsize=(8, 8))
plt.pie(df['Share'], labels=df['Company'], autopct='%1.1f%%')
plt.title('Market Share Distribution')
plt.show()
Pie chart showing market share
distribution
Output:
plt.figure(figsize=(10, 6))
df.plot(kind='area', stacked=True)
plt.title('Revenue Streams Over Time')
plt.xlabel('Date')
plt.ylabel('Revenue ($K)')
plt.show()
Output:
plt.figure(figsize=(10, 6))
df.plot(marker='o')
plt.title('Temperature Comparison Across Cities')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.grid(True)
plt.show()
Output:
Output:
df['Sales'].plot(ax=ax1, marker='o')
ax1.set_title('Monthly Sales')
ax1.set_ylabel('Sales ($)')
ax1.grid(True)
df['Profit'].plot(kind='bar', ax=ax2)
ax2.set_title('Monthly Profit')
ax2.set_ylabel('Profit ($)')
plt.tight_layout()
plt.show()
Output:
11. Heatmap - Correlation Matrix
np.random.seed(42)
n = 100
data = {
'A': np.random.normal(0, 1, n),
'B': np.random.normal(0, 1, n),
'C': np.random.normal(0, 1, n),
'D': np.random.normal(0, 1, n)
}
df = pd.DataFrame(data)
correlation = df.corr()
plt.figure(figsize=(8, 6))
plt.imshow(correlation, cmap='coolwarm', aspect='auto')
plt.colorbar()
plt.xticks(range(len(correlation.columns)), correlation.columns)
plt.yticks(range(len(correlation.columns)), correlation.columns)
plt.title('Correlation Heatmap')
plt.show()
Output:
ts.plot(ax=ax1)
ax1.set_title('Original Time Series')
ts.rolling(window=7).mean().plot(ax=ax2)
ax2.set_title('7-Day Rolling Mean')
ts.rolling(window=30).mean().plot(ax=ax3)
ax3.set_title('30-Day Rolling Mean')
plt.tight_layout()
plt.show()
Output:
Note: The actual outputs will appear when you run the code in your environment. The placeholder images are used to
indicate where the visualizations would appear.