Để tạo biểu đồ 1D, chúng ta chỉ cần một vectơ số duy nhất. Đối với biểu đồ 2D, chúng tôi sẽ cần một vectơ thứ hai. Chúng tôi sẽ tạo cả hai bên dưới và hiển thị biểu đồ cho từng vectơ
N_points = 100000 n_bins = 20 # Generate two normal distributions dist1 = rng.standard_normal[N_points] dist2 = 0.4 * rng.standard_normal[N_points] + 5 fig, axs = plt.subplots[1, 2, sharey=True, tight_layout=True] # We can set the number of bins with the *bins* keyword argument. axs[0].hist[dist1, bins=n_bins] axs[1].hist[dist2, bins=n_bins]fig, axs = plt.subplots[1, 2, tight_layout=True] # N is the count in each bin, bins is the lower-limit of the bin N, bins, patches = axs[0].hist[dist1, bins=n_bins] # We'll color code by height, but you could use any scalar fracs = N / N.max[] # we need to normalize the data to 0..1 for the full range of the colormap norm = colors.Normalize[fracs.min[], fracs.max[]] # Now, we'll loop through our objects and set the color of each accordingly for thisfrac, thispatch in zip[fracs, patches]: color = plt.cm.viridis[norm[thisfrac]] thispatch.set_facecolor[color] # We can also normalize our inputs by the total number of counts axs[1].hist[dist1, bins=n_bins, density=True] # Now we format the y-axis to display percentage axs[1].yaxis.set_major_formatter[PercentFormatter[xmax=1]] fig, ax = plt.subplots[tight_layout=True] hist = ax.hist2d[dist1, dist2]
Tùy chỉnh biểu đồ của bạn
Tùy chỉnh biểu đồ 2D tương tự như trường hợp 1D, bạn có thể kiểm soát các thành phần trực quan như kích thước thùng hoặc chuẩn hóa màu
Biểu đồ Python là gì?
Biểu đồ về cơ bản là được sử dụng để biểu thị dữ liệu được cung cấp dưới dạng một số nhóm . Đó là phương pháp chính xác để biểu diễn đồ họa phân phối dữ liệu số. Đây là một loại biểu đồ thanh trong đó trục X biểu thị phạm vi bin trong khi trục Y cung cấp thông tin về tần số.
Làm cách nào để tạo biểu đồ trong Python?
Các bước để vẽ biểu đồ trong Python bằng Matplotlib .
Bước 1. Cài đặt gói Matplotlib. .
Bước 2. Thu thập dữ liệu cho biểu đồ. .
Bước 3. Xác định số lượng thùng. .
Bước 4. Vẽ biểu đồ trong Python bằng matplotlib