10 Practical Data Visualization Scenarios with Python Code Examples
This article presents ten practical Python code examples for common data visualization scenarios—including line, scatter, bar, pie, box, heatmap, area, histogram, tree, and map visualizations—demonstrating how to use matplotlib, seaborn, and geopandas to effectively convey data insights.
When it comes to data visualization, the following ten practical code examples can help you improve the effectiveness of data analysis.
1. Line Chart – Suitable for showing trends over time or an ordered variable.
import matplotlib.pyplot as
plt.plot(x, y)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('折线图')
plt.show()2. Scatter Plot – Useful for displaying the relationship between two continuous variables or observing data distribution.
import matplotlib.pyplot as
plt.scatter(x, y)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('散点图')
plt.show()3. Bar Chart – Ideal for comparing differences between categories or groups.
import matplotlib.pyplot as
plt.bar(x, height)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('柱状图')
plt.show()4. Pie Chart – Used to show the proportion or percentage of different categories.
import matplotlib.pyplot as
plt.pie(sizes, labels=labels)
plt.title('饼图')
plt.show()5. Box Plot – Displays data distribution and helps detect outliers.
import matplotlib.pyplot as
plt.boxplot(data)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('箱线图')
plt.show()6. Heatmap – Shows correlation between two variables or density distribution of data.
import seaborn as sns
sns.heatmap(data, annot=True)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('热力图')
plt.show()7. Area Chart – Highlights cumulative effect while showing trends over time.
import matplotlib.pyplot as
plt.fill_between(x, y1, y2)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('区域图')
plt.show()8. Histogram – Shows distribution and frequency of a continuous variable.
import matplotlib.pyplot as
plt.hist(data, bins=10)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('直方图')
plt.show()9. Tree Diagram – Useful for visualizing hierarchical or organizational structures.
import matplotlib.pyplot as
plt.barh(y, width)
plt.xlabel('X轴标签')
plt.ylabel('Y轴标签')
plt.title('树状图')
plt.show()10. Map Visualization – Displays geographic data or spatial distribution.
import geopandas as gpd
data = gpd.read_file('shapefile.shp')
data.plot()
plt.title('地图可视化')
plt.show()These code examples cover common visualization types; by selecting the appropriate chart and customizing the code to your data and analysis goals, you can better understand and communicate information.
Test Development Learning Exchange
Test Development Learning Exchange
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.