Python Data Visualization: Line, Scatter, Bar, Stacked Bar, Pie, Histogram, Heatmap, Boxplot, Interactive Plotly, and DataFrame Charts
This guide demonstrates how to install common Python plotting libraries and provides ready-to-use functions for creating line, scatter, bar, stacked bar, pie, histogram, heatmap, boxplot, interactive Plotly scatter, and pandas DataFrame visualizations with example code snippets.
Installation pip install matplotlib seaborn plotly panda Simple Line Chart
import matplotlib.pyplot as plt
def plot_line_chart(x, y, title, xlabel, ylabel):
plt.plot(x, y)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.show()
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plot_line_chart(x, y, 'Simple Line Chart', 'X-axis', 'Y-axis')Scatter Chart
import matplotlib.pyplot as plt
def plot_scatter_chart(x, y, title, xlabel, ylabel):
plt.scatter(x, y)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.show()
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plot_scatter_chart(x, y, 'Scatter Chart', 'X-axis', 'Y-axis')Bar Chart
import matplotlib.pyplot as plt
def plot_bar_chart(labels, values, title):
plt.bar(labels, values)
plt.title(title)
plt.show()
labels = ['A', 'B', 'C', 'D', 'E']
values = [10, 20, 15, 25, 18]
plot_bar_chart(labels, values, 'Bar Chart')Stacked Bar Chart
import matplotlib.pyplot as plt
def plot_stacked_bar_chart(labels, data, title):
plt.bar(labels, data[0], label='Data 1')
plt.bar(labels, data[1], bottom=data[0], label='Data 2')
plt.title(title)
plt.legend()
plt.show()
labels = ['A', 'B', 'C', 'D', 'E']
data = [[10, 20, 15, 25, 18], [5, 10, 7, 12, 9]]
plot_stacked_bar_chart(labels, data, 'Stacked Bar Chart')Pie Chart
import matplotlib.pyplot as plt
def plot_pie_chart(labels, sizes, title):
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title(title)
plt.show()
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 15]
plot_pie_chart(labels, sizes, 'Pie Chart')Histogram
import matplotlib.pyplot as plt
def plot_histogram(data, title):
plt.hist(data, bins=10)
plt.title(title)
plt.show()
data = [21, 22, 23, 4, 5, 6, 77, 8, 9, 10, 31, 32, 33, 34, 35, 36, 37, 18, 49, 50, 51]
plot_histogram(data, 'Histogram')Heatmap
import seaborn as sns
import numpy as np
def plot_heatmap(data, title):
sns.heatmap(data, annot=True, fmt="d")
plt.title(title)
plt.show()
data = np.random.rand(5, 5)
plot_heatmap(data, 'Heatmap')Boxplot
import matplotlib.pyplot as plt
def plot_boxplot(data, title):
plt.boxplot(data)
plt.title(title)
plt.show()
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plot_boxplot(data, 'Boxplot')Interactive Scatter Plot (Plotly)
import plotly.express as px
def plot_interactive_scatter(x, y, title):
fig = px.scatter(x=x, y=y, title=title)
fig.show()
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plot_interactive_scatter(x, y, 'Interactive Scatter Plot')Pandas DataFrame Plot
import pandas as pd
import matplotlib.pyplot as plt
def plot_dataframe(df, x_col, y_col, title):
df.plot(x=x_col, y=y_col, kind='line')
plt.title(title)
plt.show()
data = {'Year': [2000, 2001, 2002, 2003, 2004],
'Sales': [150, 200, 250, 300, 350]}
df = pd.DataFrame(data)
plot_dataframe(df, 'Year', 'Sales', 'DataFrame Plot')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.
