如何在散點圖中以均勻間隔更改 x 軸值? (How to change x axis value with even interval in scatter plot?)


問題描述

如何在散點圖中以均勻間隔更改 x 軸值? (How to change x axis value with even interval in scatter plot?)

x = [101.52762499 105.79521102 103.5158705   93.55296605 108.73719223 98.57426097  98.73552014  79.88138657  91.71042366 114.15815465]
y = [107.83168825  93.11360106 102.49196148  97.84879532 114.41714004 97.39079067 111.35664058  76.97523782  88.63047332  90.11216039]

我想做一個散點圖,它顯示回歸線,其中 x 值均勻地跨越 100 個區間,從 x 數據的最小值到最大值。

我該用什麼代碼需要用來改變 x 軸嗎?

m,c = np.polyfit(x,y,1) #this is to find the best fit line
plt.plot(x, m*x + c) # this to plot the best fit line
plt.plot(x,y,'o') # this is to plot in 

我厭倦了使用 plt.xticks(0,200) 但它給了我一個錯誤信息

TypeError: object 'int' 類型的沒有 len()


參考解法

方法 1:

The following code puts x ticks to create 100 equally‑sized intervals between the first and last value.

import numpy as np
from matplotlib import pyplot as plt

x = np.array([101.52762499, 105.79521102, 103.5158705, 93.55296605, 108.73719223, 98.57426097, 98.73552014, 79.88138657, 91.71042366, 114.15815465])
y = np.array([107.83168825, 93.11360106, 102.49196148, 97.84879532, 114.41714004, 97.39079067, 111.35664058, 76.97523782, 88.63047332, 90.11216039])

m, c = np.polyfit(x, y, 1) # this is to find the best fit line
plt.figure(figsize=(15, 4))
plt.plot(x, m * x + c) # this to plot the best fit line
plt.plot(x, y, 'o') # this is to plot in

bins = np.linspace(x.min(), x.max(), 101) # 100 equally‑sized intervals
plt.xticks(bins, rotation=90)
plt.grid(True, axis='x') # the grid lines show the 100 intervals of the xticks
plt.margins(x=0.02) # less whitespace left and right
plt.tight_layout()
plt.show()
</code></pre>

resulting plot

(by GenJohanC)

參考文件

  1. How to change x axis value with even interval in scatter plot? (CC BY‑SA 2.5/3.0/4.0)

#scatter-plot #Python #matplotlib






相關問題

matplotlib:重繪前清除散點數據 (matplotlib: clearing the scatter data before redrawing)

使用 matplotlib 保存散點圖動畫 (Saving scatterplot animations with matplotlib)

在 matplotlib 中圍繞散點圖中的數據點繪製平滑多邊形 (draw a smooth polygon around data points in a scatter plot, in matplotlib)

Java:非常簡單的散點圖實用程序 (Java: Really simple scatter plot utility)

如何在多個圖中選擇一個要編輯的圖? (How to select one plot to be edit in multiple plots?)

d3js散點圖自動更新不起作用 (d3js scatter plot auto update doesnt work)

散點圖中的重疊趨勢線,R (Overlapping Trend Lines in scatterplots, R)

DC.JS 散點圖選擇 (DC.JS scatterplot chart selection)

固定散景散點圖中的軸間距(刻度) (Fixing axis spacing (ticks) in Bokeh scatter plots)

如何在顏色條上繪製散點圖? (How to plot scatter plot points on a colorbar?)

如何在散點圖中以均勻間隔更改 x 軸值? (How to change x axis value with even interval in scatter plot?)

如何在散點圖(地理地圖)上標註數據? (How to annotate data on the scatter plot (geo map)?)







留言討論