Python Data Analysis and Visualization Examples Using Pandas, Matplotlib, Seaborn, ReportLab, and Plotly/Dash
This article presents a series of Python code examples that demonstrate how to generate statistical reports, pivot tables, various charts, heatmaps, PDF reports, and an interactive dashboard for sales data analysis using libraries such as pandas, matplotlib, seaborn, reportlab, and plotly/dash.
1. Basic Statistical Report – Generate a report containing count, mean, standard deviation, etc., for a dataset.
import pandas as pd
# Load data
df = pd.read_csv('data.csv')
# Generate descriptive statistics report
report_stats = df.describe()
print(report_stats)2. Pivot Table – Use a pivot table to summarize data across different dimensions.
pivot_table = pd.pivot_table(df, values='Sales', index=['Year', 'Category'], aggfunc=np.sum)
print(pivot_table)3. Bar Chart for Categorical Data – Compare sales volumes of different products.
import matplotlib.pyplot as plt
product_sales = df['Product'].value_counts()
product_sales.plot(kind='bar')
plt.title('Product Sales Comparison')
plt.xlabel('Product')
plt.ylabel('Sales Count')
plt.show()4. Pie Chart for Proportion – Show the distribution of customer source channels.
channel_counts = df['Source_Channel'].value_counts(normalize=True)
channel_counts.plot(kind='pie', autopct='%1.1f%%')
plt.title('Customer Source Channel Distribution')
plt.show()5. Line Chart for Trend – Visualize monthly sales trends.
df['Month'] = df['Date'].dt.month
monthly_sales = df.groupby('Month')['Sales'].sum()
monthly_sales.plot(kind='line')
plt.title('Monthly Sales Trend')
plt.xlabel('Month')
plt.ylabel('Total Sales')
plt.show()6. Heatmap for Variable Relationships – Visualize a correlation matrix.
import seaborn as sns
correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix Heatmap')
plt.show()7. Boxplot for Outlier Detection – Detect abnormal values in sales data.
sns.boxplot(x=df['Sales'])
plt.title('Sales Data Outlier Detection')
plt.show()8. Grouped Bar Chart – Compare sales of each product category across different years.
sns.catplot(x='Category', y='Sales', hue='Year', data=df, kind='bar')
plt.title('Annual Category Sales Comparison')
plt.show()9. Custom PDF Report – Generate a PDF that includes charts and tables using ReportLab.
from reportlab.lib.pagesizes import letter, landscape
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
doc = SimpleDocTemplate('report.pdf', pagesize=landscape(letter))
story = []
story.append(Paragraph('Sales Report', style))
img_data = open('chart.png', 'rb').read()
img = Image(img_data, width=400, height=300)
story.append(img)
table_data = [df.columns.tolist()] + df.values.tolist()
t = Table(table_data)
t.setStyle(TableStyle([
('BACKGROUND', (0,0), (-1,0), '#CCCCCC'),
('TEXTCOLOR',(0,0),(-1,0), '#000000'),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
('FONTSIZE', (0,0), (-1,0), 14)
]))
story.append(t)
doc.build(story)10. Interactive Dashboard (Plotly/Dash) – Build a web dashboard to explore multi‑dimensional sales data.
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(id='year-selector', options=[{'label': i, 'value': i} for i in df['Year'].unique()], value=df['Year'].min()),
dcc.Graph(id='sales-graph')
])
@app.callback(Output('sales-graph', 'figure'), Input('year-selector', 'value'))
def update_graph(selected_year):
filtered_df = df[df['Year'] == selected_year]
fig = px.bar(filtered_df, x='Category', y='Sales', title=f'Sales by Category in {selected_year}')
return fig
if __name__ == '__main__':
app.run_server(debug=True)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
