Hướng dẫn visualizing large data sets python matplotlib - trực quan hóa tập dữ liệu lớn python matplotlib

Trong bài viết trước của tôi, chúng tôi đã thảo luận về một vài số liệu hiệu suất và các tình huống mà chúng có thể được sử dụng. Trong bài đăng hôm nay, chúng tôi sẽ khám phá một thư viện âm mưu 2 chiều phổ biến được gọi là matplotlib.Matplotlib.

Đối với mục đích tham khảo, có thể tìm thấy tài liệu chính thức của phiên bản matplotlib 3.1.1 (ổn định) ở đây- https://matplotlib.org/3.1.1/users/index.html


Matplotlib là gì?

Matplotlib là một thư viện khổng lồ có khoảng 70.000 dòng mã. Nó được lấy cảm hứng từ phần mềm MathWorks của Matlab. Nó được xây dựng trên Numpy và phục vụ để làm việc trên Scipy. Đây là một trong những thư viện âm mưu mạnh mẽ nhất để làm việc trong Python và Numpy.MATLAB. It is built on NumPy and caters to work on SciPy. It is one of the most powerful plotting libraries to work in Python and Numpy.

Hướng dẫn visualizing large data sets python matplotlib - trực quan hóa tập dữ liệu lớn python matplotlib

Người ta thường thấy rằng chúng ta có xu hướng nhớ hình ảnh và hình ảnh nhiều hơn lời nói. Trên cùng một dòng, việc lấy hiểu biết sâu sắc từ dữ liệu được biểu diễn trực quan hơn so với dữ liệu được lưu trữ trong các định dạng của Excel, CSV, Word hoặc bất kỳ dạng tài liệu văn bản nào. Đây là nơi vẽ các thư viện đi vào hình ảnh.

Trong các bài viết trước của tôi, tôi đã trực quan hóa dữ liệu bằng cách sử dụng matplotlib để dễ hiểu dữ liệu, bất thường (dị thường) và xu hướng trong dữ liệu. Matplotlib có thể được sử dụng để thể hiện các ô, lô thanh, biểu đồ, sơ đồ phân tán và nhiều hơn nữa.Matplotlib so that it would be easy to understand the data, the abnormalities (anomalies), and trends in the data. Matplotlib can be used to represent line plots, bar plots, histograms, scatter plots and much more.

Thư viện này có thể được cài đặt với lệnh sau:

pip install matplotlib

Và để sử dụng thư viện trong mã Python của bạn, hãy sử dụng câu lệnh sau để nhập mô -đun,

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt

Nó có một số phần cho nó, cụ thể là:

  1. Figure(): Nó bao gồm toàn bộ trực quan, chứa các trục x và y.

  2. Axes(): Đây là các ô, trục X, Y và Z (trục Z trong một số trường hợp nhất định). These are the plots, the X, Y and Z axes (Z axis in certain cases).

  3. Axis(): Điều này chứa các số và trợ giúp trong việc tạo ra các giới hạn của biểu đồ. This contains the numbers and help in generating the limits of the graph.

  4. Artist(): Các đối tượng trực quan được gắn với các trục, chẳng hạn như 'văn bản', 'line2d' và 'thu thập'. The visual objects which are tied to axes, such as 'Text', 'Line2D', and 'collection'.

  5. import matplotlib.pyplot as plt
    
    # or
    
    from matplotlib import pyplot as plt
    0,
    import matplotlib.pyplot as plt
    
    # or
    
    from matplotlib import pyplot as plt
    1: Hai thành phần của matplotlib, được sử dụng để gắn nhãn các điểm đánh dấu của trục X và Y.

  6. import matplotlib.pyplot as plt
    
    # or
    
    from matplotlib import pyplot as plt
    2: Thành phần giúp đặt tên các biến quan sát.The component which helps in naming the observation variables.

Khi biểu đồ được vẽ, các trục X và Y được điều chỉnh để lấy mặc định

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
3 và
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
4, nhưng các giá trị này có thể được tùy chỉnh theo yêu cầu của một người.x and y axes are adjusted to take the default
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
3 and
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
4, but these values can be customized as per one's requirements.

Chúng tôi sẽ bắt đầu bằng cách sử dụng

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
5, đây là mô -đun matplotlib sẽ giúp chúng tôi thêm các phần tử cốt truyện như dòng, hình ảnh, văn bản vào các trục.

import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()

Đầu ra của mã:

Hướng dẫn visualizing large data sets python matplotlib - trực quan hóa tập dữ liệu lớn python matplotlib

Lưu ý: Luôn nhớ chạy chức năng

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
6 và hàm
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
7 cùng nhau.
Always remember to run the
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
6 function and the
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
7 function together.

Giải thích về mã trên:

Trong mã trên, chúng tôi đã nhập các gói cần thiết để vẽ đồ thị. Chúng tôi gọi hàm

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
8 bằng cách chuyển 2 mảng cho nó và sau đó gọi hàm
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
9 để gọi biểu đồ đồ thị. Mảng đầu tiên sẽ được vẽ theo trục X và mảng thứ hai sẽ được vẽ theo trục y. Tiêu đề và tên cho trục x và y có thể được thêm vào bằng cách sử dụng các hàm
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
0,
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
1 và
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
2.2 arrays to it and then calling the
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
9 function to invoke the graph plotting. The first array will be plotted against the x-axis and the second array will be plotted against the y-axis. The title and names for x and y-axis can be added using the functions
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
0,
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
1 and
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
2.


Sử dụng phương thức import matplotlib.pyplot as plt import numpy as np plt.plot([2,4,6,8], [3,1,5,0]) plt.title('My first plot') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()3:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize = (8,5))
plt.plot([2,4,6,8], [1,2,3,4])
plt.show()

Đầu ra của mã:

Hướng dẫn visualizing large data sets python matplotlib - trực quan hóa tập dữ liệu lớn python matplotlib

Giải thích về mã trên:

Trong mã trên, chúng tôi đã nhập các gói cần thiết để vẽ đồ thị. Chúng tôi gọi hàm

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
8 bằng cách chuyển 2 mảng cho nó và sau đó gọi hàm
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
9 để gọi biểu đồ đồ thị. Mảng đầu tiên sẽ được vẽ theo trục X và mảng thứ hai sẽ được vẽ theo trục y. Tiêu đề và tên cho trục x và y có thể được thêm vào bằng cách sử dụng các hàm
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
0,
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
1 và
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
2.8 is the length and 5 is the breadth of the plot.


Sử dụng phương thức import matplotlib.pyplot as plt import numpy as np plt.plot([2,4,6,8], [3,1,5,0]) plt.title('My first plot') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()3:

Trong mã này, phương pháp

import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
3 đã được sử dụng để chỉ định kích thước của lô. Ở đây, các tham số của
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
3 bao gồm chiều dài và chiều rộng của lô, tức là 8 là chiều dài và 5 là chiều rộng của lô.a solid blue line. Other colors can be go (green), red, yellow, orange.

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize = (8,5))
plt.plot([2,4,6,8], "green")
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()

Đầu ra của mã:

Hướng dẫn visualizing large data sets python matplotlib - trực quan hóa tập dữ liệu lớn python matplotlib

Lưu ý: Luôn nhớ chạy chức năng

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
6 và hàm
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
7 cùng nhau.What if I wish to plot more than one visualization in a single graph?

Giải thích về mã trên:Yes, it is possible. The below code example shows the same:

Trong mã trên, chúng tôi đã nhập các gói cần thiết để vẽ đồ thị. Chúng tôi gọi hàm

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
8 bằng cách chuyển 2 mảng cho nó và sau đó gọi hàm
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
9 để gọi biểu đồ đồ thị. Mảng đầu tiên sẽ được vẽ theo trục X và mảng thứ hai sẽ được vẽ theo trục y. Tiêu đề và tên cho trục x và y có thể được thêm vào bằng cách sử dụng các hàm
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
0,
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
1 và
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
2.

import matplotlib.pyplot as plt
import numpy as np

# Sample data generation
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, axs = plt.subplots(2)

fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(-x, -y)
plt.show()

Giải thích về mã trên:

Trong mã trên, chúng tôi đã nhập các gói cần thiết để vẽ đồ thị. Chúng tôi gọi hàm

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
8 bằng cách chuyển 2 mảng cho nó và sau đó gọi hàm
import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
9 để gọi biểu đồ đồ thị. Mảng đầu tiên sẽ được vẽ theo trục X và mảng thứ hai sẽ được vẽ theo trục y. Tiêu đề và tên cho trục x và y có thể được thêm vào bằng cách sử dụng các hàm
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
0,
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
1 và
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
2.

Sử dụng phương thức

import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
3:subplot() method takes three arguments, they are
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize = (8,5))
plt.plot([2,4,6,8], [1,2,3,4])
plt.show()
1,
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize = (8,5))
plt.plot([2,4,6,8], [1,2,3,4])
plt.show()
2 and
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize = (8,5))
plt.plot([2,4,6,8], [1,2,3,4])
plt.show()
3. They indicate the number of rows, number of columns and the index number of the sub-plot.

Trong mã này, phương pháp

import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
3 đã được sử dụng để chỉ định kích thước của lô. Ở đây, các tham số của
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
3 bao gồm chiều dài và chiều rộng của lô, tức là 8 là chiều dài và 5 là chiều rộng của lô.bar graph, pie chart, histogram, scatter plots and 3-D plotting can be represented.


Màu sắc của cốt truyện và dòng

Trong hàm

import matplotlib.pyplot as plt

# or

from matplotlib import pyplot as plt
8, tham số thứ ba có thể được chỉ định, là một chuỗi có thể được sử dụng để chỉ ra loại màu và dòng của lô. Ví dụ: mặc định là
import matplotlib.pyplot as plt
import numpy as np

plt.plot([2,4,6,8], [3,1,5,0])
plt.title('My first plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
7, là một đường màu xanh rắn. Các màu khác có thể là đi (màu xanh lá cây), đỏ, vàng, cam.

  • Câu hỏi tiếp theo của bạn có thể là, nếu tôi muốn vẽ nhiều hơn một trực quan hóa trong một biểu đồ?

  • Câu trả lời là, vâng, nó là có thể. Ví dụ mã dưới đây cho thấy giống nhau:

  • Trong ví dụ mã ở trên. Hãy thử mã

    import matplotlib.pyplot as plt
    
    # or
    
    from matplotlib import pyplot as plt
    7 từ một dòng và nhận xét trong hai dòng còn lại, bạn sẽ thấy cốt truyện khác nhau mỗi lần. Một minh họa khác về cách nhiều lô có thể được biểu diễn trong một ô duy nhất:

Bạn cũng có thể thích:

  • Giới thiệu về Gói điện toán khoa học cho Python cho Python
  • Python bất kỳ () và tất cả () các phương thức - Chức năng thư viện Python
  • Chuyển đổi phạm vi số chuỗi bằng dấu gạch nối thành danh sách các số trong Python
  • Tăng cường độ sáng ngẫu nhiên

Làm thế nào để bạn hình dung một bộ dữ liệu lớn trong Python?

Để có được các tính năng đó giúp bạn giải thích dữ liệu, chúng tôi có thể nhúng các hình ảnh được tạo bởi Datashader vào một cốt truyện. Cách dễ nhất là sử dụng holoviews, đây là API âm mưu cấp cao cung cấp tính linh hoạt để sử dụng matplotlib, bokeh hoặc âm mưu làm phụ trợ.embed the images generated by Datashader into a plot. The easiest way is to use HoloViews, which is a high-level plotting API that provides the flexibility to use either Matplotlib, Bokeh, or Plotly as the backend.

Làm thế nào để bạn trực quan hóa một tập dữ liệu lớn?

Kỹ thuật trực quan hóa dữ liệu tốt nhất cho dữ liệu nhỏ và lớn..
Line Lô.....
Biểu đồ cột.....
Biểu đồ bánh và bánh rán.....
Biểu đồ biểu đồ.....
Cốt truyện phân tán.....
Ước tính mật độ hạt nhân cho dữ liệu không tham số.....
Box và râu ria cho dữ liệu lớn.....
Các đám mây từ và sơ đồ mạng cho dữ liệu phi cấu trúc ..

Có thể xử lý bao nhiêu điểm matplotlib?

Thú vị.Như câu trả lời của Jonathan Dursi đề cập, 20 triệu điểm có thể đạt được với matplotlib, nhưng với một số ràng buộc (đầu ra raster, Hồi).20 million points is achievable with Matplotlib, but with some constraints (raster output,…).

Có điều gì tốt hơn matplotlib không?

Plotly có một số lợi thế so với matplotlib.Một trong những lợi thế chính là chỉ có một vài dòng mã là cần thiết để tạo ra các ô tương tác, dễ chịu về mặt thẩm mỹ.Tính tương tác cũng cung cấp một số lợi thế so với các sơ đồ matplotlib tĩnh: tiết kiệm thời gian khi ban đầu khám phá bộ dữ liệu của bạn.. One of the main advantages is that only a few lines of codes are necessary to create aesthetically pleasing, interactive plots. The interactivity also offers a number of advantages over static matplotlib plots: Saves time when initially exploring your dataset.