如何在不使用数据中的 x 值的情况下绘制重复的 x 轴值?

问题描述 投票:0回答:1

我知道已经回答了类似的问题(如何在Python中绘制x轴值多次出现的位置,例如0 1 2 3 2 1 0),但是在该问题中,x值用作x - 轴值。 主要区别是我的数据点 (x) 彼此不等距(请参阅下面的 data_df),并且我想在固定的 x 轴上绘制,与 x 值不严格相关。我的测量窗口从 3.5 到 4.9,然后从 4.9 回到 3.5,所以例如

range = [3.5, 3.6,...,4.8, 4.9, 4.8,..., 3.6, 3.5]

我想要做的是将“范围”作为我的x轴并根据该轴绘制我的data_df。 我想我想要的是 this semi-goal image.

但是,在一张图中(一个 x 轴和 y 轴)而不是两张单独的图。

我觉得我已经尝试了迄今为止在 stackoverflow 上能找到的所有东西,但我想我对 python 和 matplot 的有限经验阻碍了我。这是我迄今为止尝试过的:

data_df 是这个 DataFrame:

[[4.2, 416394000.0], [4.55, 417501000.0], [4.68, 418680000.0], [4.73, 417273000.0], [4.76, 416939000.0],
 [4.77, 417441000.0], [4.78, 418144000.0], [4.79, 418832000.0], [4.78, 419445000.0], [4.79, 419811000.0],
 [4.8, 419832000.0], [4.81, 419727000.0], [4.8, 419669000.0], [4.8, 419691000.0], [4.8, 419676000.0],
 [4.81, 419775000.0], [4.81, 419698000.0], [4.81, 419729000.0], [4.81, 419684000.0], [4.82, 419435000.0],
 [4.82, 419201000.0], [4.81, 418755000.0], [4.83, 418273000.0], [4.84, 413020000.0], [4.84, 411498000.0],
 [4.85, 409950000.0], [4.86, 407930000.0], [4.87, 405948000.0], [4.88, 403255000.0], [4.89, 401120000.0],
 [4.9, 399124000.0], [4.67, 401493000.0], [4.65, 401813000.0], [4.63, 401349000.0], [4.62, 399976000.0],
 [4.61, 398104000.0], [4.6, 395457000.0], [4.59, 392566000.0], [4.58, 389359000.0], [4.57611, 385742000.0],
 [4.56588, 381776000.0], [4.5568, 377795000.0], [4.54503, 373427000.0], [4.53287, 369763000.0],
 [4.51665, 365492000.0], [4.49696, 61393000.0], [4.47187, 357905000.0], [4.44002, 353489000.0],
 [4.39678, 349760000.0], [4.33945, 346221000.0], [4.2659, 343530000.0], [4.17517, 342173000.0],
 [4.04198, 341340000.0], [3.87693, 340104000.0], [3.74161, 333359000.0], [3.41905, 328130000.0]]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data_df = ...

#creating xticks
ticks = np.arange(3.5, 4.9, 0.1)
ticks = np.append(ticks, np.arange(4.8, 3.5, -0.1)

#plotting
fig = plt.figure(figsize=(10,7.5))
ax = plt.subplot()
plt.xticks(ticks)
ax.scatter(data_df.x, data_df.y)
plt.show()

我期望

plt.xticks
按顺序遍历刻度并将所有值设置为单独的刻度。然而,我得到了这个:this.

不是遍历每个元素并将其添加到 x 轴,而是重复从 4.8 到 3.6 的值。

我还尝试将 data_df 手动拆分为两个范围(3.5-4.9 和 4.9 到 3.5),并将它们绘制在一张图中。不同的方式,相同的结果。

我也用

ax.set_xticks
ax.set_xticklabels
尝试过,但没有成功。

非常感谢您的帮助:)

python matplotlib plot axis
1个回答
0
投票

使用

ax.set_xticks
ax.set_xticklabels
似乎是正确的方法。例如,

import numpy as np
from matplotlib import pyplot as plt

df = np.asarray(
    [
        [4.2, 416394000.0], [4.55, 417501000.0], [4.68, 418680000.0], [4.73, 417273000.0], [4.76, 416939000.0],
        [4.77, 417441000.0], [4.78, 418144000.0], [4.79, 418832000.0], [4.78, 419445000.0], [4.79, 419811000.0],
        [4.8, 419832000.0], [4.81, 419727000.0], [4.8, 419669000.0], [4.8, 419691000.0], [4.8, 419676000.0],
        [4.81, 419775000.0], [4.81, 419698000.0], [4.81, 419729000.0], [4.81, 419684000.0], [4.82, 419435000.0],
        [4.82, 419201000.0], [4.81, 418755000.0], [4.83, 418273000.0], [4.84, 413020000.0], [4.84, 411498000.0],
        [4.85, 409950000.0], [4.86, 407930000.0], [4.87, 405948000.0], [4.88, 403255000.0], [4.89, 401120000.0],
        [4.9, 399124000.0], [4.67, 401493000.0], [4.65, 401813000.0], [4.63, 401349000.0], [4.62, 399976000.0],
        [4.61, 398104000.0], [4.6, 395457000.0], [4.59, 392566000.0], [4.58, 389359000.0], [4.57611, 385742000.0],
        [4.56588, 381776000.0], [4.5568, 377795000.0], [4.54503, 373427000.0], [4.53287, 369763000.0],
        [4.51665, 365492000.0], [4.49696, 61393000.0], [4.47187, 357905000.0], [4.44002, 353489000.0],
        [4.39678, 349760000.0], [4.33945, 346221000.0], [4.2659, 343530000.0], [4.17517, 342173000.0],
        [4.04198, 341340000.0], [3.87693, 340104000.0], [3.74161, 333359000.0], [3.41905, 328130000.0]
    ]
)

# "mid" point in x
mid = 4.9

# get xvalues
x = df[:, 0]

# get index of the mid-point
midp = np.argwhere(x == mid)[0][0]

# create version of x axis that extends above the mid-point
x[midp + 1:] = 2 * mid - x[midp + 1:]

# create plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.scatter(x, df[:, 1])

# set new x-axis limits
ax.set_xlim([3.5, mid + (mid - 3.5)])

# set new x-tick positions and labels
xticks = np.arange(3.5, mid + (mid - 3.5), 0.2)
xticklabels = []

for xt in xticks:
    if xt > mid:
        # correct the label above the mid-point
        xticklabels.append(f"{2 * mid - xt:.1f}")
    else:
        xticklabels.append(f"{xt:.1f}")

ax.set_xticks(xticks, labels=xticklabels)

plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.