How to build graphs in Python?

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

The Ultimate Guide to Data Visualization

Data visualization is a crucial tool for information analysis. Graphs help quickly understand trends, identify anomalies, and draw informed conclusions. Python offers a wide range of libraries for creating visualizations.

Why Use Python for Graphing?

Creating graphs in Python is essential for solving the following tasks:

  • Rapid Analysis of Large Data Sets: Accelerate data processing and insight generation.
  • Visual Representation of Experiment and Research Results: Clearly present findings in an understandable format.
  • Data Presentation to Clients and Colleagues: Enhance communication and stakeholder understanding.
  • Improved Informational Content of Reports and Dashboards: Create compelling and informative reports.
  • Identification of Hidden Patterns in Data: Discover insights that might be missed in raw data.
  • Creation of Interactive Visualizations for Web Applications: Provide engaging and dynamic data exploration.

Overview of Popular Visualization Libraries

Among the most popular tools for building graphs in Python are:

  • Matplotlib: A foundational visualization library with a low level of complexity. Provides complete control over the appearance of graphs.
  • Pyplot: A module of the Matplotlib library for quickly creating standard graphs. Known for its ease of use.
  • Seaborn: A library for statistical visualization, functioning as a superstructure over Matplotlib. Has a medium level of complexity.
  • Plotly: A tool for creating interactive graphs with a medium level of complexity.
  • Pandas Plot: Built-in visualization capabilities from DataFrame with a low level of complexity.

Working with Matplotlib and Pyplot

Installation and Import

To get started with Matplotlib, you need to install the library:

pip install matplotlib

Then import the necessary modules:

import matplotlib.pyplot as plt

Creating a Simple Line Graph

Basic example of building a line graph:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y)
plt.title("Simple Line Graph")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()

The result shows a linear relationship between the variables X and Y.

Customizing the Style and Appearance of the Graph

To improve the appearance of the graph, you can use various parameters:

plt.plot(x, y, color='green', linestyle='--', marker='o')
plt.title("Customized Graph")
plt.grid(True)
plt.show()

Main customization parameters:

  • color: Line color
  • linestyle: Line style (-- dashed, -. dash-dotted, : dotted)
  • marker: Marker for designating points (o, s, ^, v)
  • linewidth: Line thickness
  • alpha: Transparency

Creating Bar and Pie Charts

Building a Bar Chart

Bar charts are effective for comparing values between categories:

categories = ['A', 'B', 'C', 'D']
values = [10, 24, 36, 40]

plt.bar(categories, values, color='skyblue')
plt.title("Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()

Creating a Pie Chart

Pie charts show the proportion of each category to the overall whole:

sizes = [40, 30, 20, 10]
labels = ['Python', 'Java', 'C++', 'Ruby']

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.title("Pie Chart")
plt.axis('equal')  # ensures a circular shape
plt.show()

Pie chart parameters:

  • autopct: Percentage display format
  • startangle: Starting angle of the first sector
  • explode: Highlighting specific sectors

Building Distribution Histograms

Histograms show the distribution of data across intervals:

import numpy as np

data = np.random.randn(1000)
plt.hist(data, bins=30, color='orange', alpha=0.7)
plt.title("Distribution Histogram")
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.show()

Histogram Parameter Configuration

For precise histogram settings, the following parameters are used:

  • bins: Number of intervals
  • density: Data normalization
  • cumulative: Cumulative histogram
  • orientation: Orientation (horizontal/vertical)

Using Seaborn for Statistical Visualization

Installation and Import of Seaborn

pip install seaborn
import seaborn as sns
import pandas as pd

Creating a Scatter Plot

Seaborn provides a more modern and aesthetic approach to visualization:

# DataFrame example
data = pd.DataFrame({
    "Age": [25, 30, 45, 50, 23, 37, 31],
    "Salary": [50000, 60000, 80000, 90000, 45000, 70000, 65000]
})

sns.scatterplot(x="Age", y="Salary", data=data)
plt.title("Scatter Plot (Seaborn)")
plt.show()

Additional Seaborn Features

Seaborn includes specialized functions for statistical visualization:

  • sns.boxplot(): Box plots
  • sns.violinplot(): Violin plots
  • sns.heatmap(): Heatmaps
  • sns.pairplot(): Scatter plot matrix

Interactive Charts with Plotly

Installation and Basics of Working with Plotly

Interactive charts are useful for dashboards and web applications:

pip install plotly
import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
                 color="species", title="Interactive Plotly Graph")
fig.show()

Advantages of Interactive Charts

Plotly provides the following features:

  • Scaling and panning
  • Tooltips
  • Data animation
  • Export to various formats
  • Integration with web applications

Visualizing Data from Pandas DataFrame

Fast Visualization with Built-in Methods

Pandas provides convenient methods for quick data visualization:

import pandas as pd

data = pd.Series([1, 3, 5, 7, 9])
data.plot(kind='line', title="Graph from Pandas")
plt.show()

Types of Graphs in Pandas

Pandas supports various types of visualization:

  • kind='line': Line graph
  • kind='bar': Bar chart
  • kind='hist': Histogram
  • kind='box': Box plot
  • kind='scatter': Scatter plot

Saving and Exporting Graphs

Basic Saving Methods

To save graphs to a file, use the savefig() function:

plt.plot(x, y)
plt.savefig("my_graph.png")

Supported Formats

Matplotlib supports various export formats:

  • .png: High-quality raster format
  • .jpg: Compressed raster format
  • .svg: Vector format
  • .pdf: PDF document
  • .eps: Vector format for publications

Export Quality Setting

plt.savefig("graph.png", dpi=300, bbox_inches='tight')

Frequently Asked Questions

Choosing the Best Library

For simple static graphs, it is recommended to use Matplotlib or Seaborn. For interactive visualizations, Plotly is more suitable.

Differences between Pyplot and Pyplotlib

Pyplot is a module of the Matplotlib library for quickly working with graphs. The term "pyplotlib" is a common error.

Resizing the Graph

plt.figure(figsize=(10, 5))
plt.plot(x, y)
plt.show()

Creating Multiple Graphs on One Canvas

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.subplot(1, 2, 2)
plt.bar(categories, values)
plt.show()

Setting Fonts and Text Size

plt.title("Title", fontsize=16)
plt.xlabel("X Axis", fontsize=12)
plt.ylabel("Y Axis", fontsize=12)

Troubleshooting Display Issues

If the graph is not displayed, make sure to use the plt.show() command after all visualization commands. In some IDEs, the graph will not be displayed without this command.

Conclusion

Building graphs in Python with various libraries opens up wide opportunities for data analysis. The choice of tool depends on the specific tasks and project requirements.

For simple graphs, use Matplotlib and Pyplot. Use Seaborn for statistical analysis. Choose Plotly for interactive visualizations.

Experiment with different types of graphs, customize their appearance and functionality. This will help make data analysis clear and professional.

News