使图形跳过一定量的x值Python

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

我正在尝试编写一段代码,使 Python 跳过图表上的某些 x 值,而不压缩它前面的 x 值。

import matplotlib.pyplot as plt
import numpy as np

#de y waarde vragen en convergeren van string naar float
x1 = float(input("Voer geluidssterkte op afstand 1 in:"))

x2 = float(input("Voer geluidssterkte op afstand 2 in:"))

x3 = float(input("Voer geluidssterkte op afstand 3 in:"))

x4 = float(input("Voer geluidssterkte op afstand 4 in:"))


#x-as waarde
x = [5 , 10, 15, 300]
#y-as waarde
y = [x1, x2 ,x3, x4]


#grafiek plotten
plt.plot(x, y, color = 'blue', linewidth = 1, marker = 'o', markerfacecolor = 'red', markersize = 5)

#Python forceren om alleen de nodige x waarden te weergeven
plt.xticks(np.arange(5, 300, 5))

#x-as limiet opstellen
plt.xlim(0, 300)


#naam x-as
plt.xlabel('afstand (cm)')
#naam y-as
plt.ylabel('geluidssterkte (dB)')

#naam van de grafiek
plt.title('geluidssterkte')


plt.show()

enter image description here

我想让它看起来像这样,但不是 20 400 在 x 轴的右侧。

import matplotlib.pyplot as plt
import numpy as np

#de y waarde vragen en convergeren van string naar float
x1 = 123
x2 = 432
x3 = 424
x4 = 243

#x-as waarde
x = [5, 10, 15, 20, 300]
#y-as waarde
y = [x1, x2, x3, x4]

#grafiek plotten
fig, ax1 = plt.subplots()

ax1.plot((x[:4]), y, color = 'blue', linewidth = 1, marker = 'o', markerfacecolor = 'red', markersize = 5)
ax1.set_xlabel('afstand (cm)')
ax1.set_ylabel('geluidssterkte (dB)')

#extra x-as toevoegen voor x = 
ax2 = ax1.twiny()
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks([20, 300])
ax2.set_xticklabels(['20', '300'])
ax2.set_xlabel('Afstand (cm)')

# Naam van de grafiek
plt.title('Geluidssterkte')

plt.show()

enter image description here

有谁知道怎么做吗?

python graph anaconda spyder x-axis
1个回答
0
投票

使用Photoshop。 或者它的道德等价物, 你问枕头的地方 PIL库删除一个 的垂直条带 “完成”输出图表.PNG

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