Python 将字符串添加到整数的x轴
我在x轴上画了一个图表(溶液浓度)和效率(y)。我将此设置为在0到100之间为x显示,但我想添加另一个数据点作为控件,而没有任何解决方案。我遇到了一些问题,因为这实际上不适合浓度轴上的任何位置,但我想在0之前或100之后添加它,可能在轴上有一个断点来分隔它们。所以我的x轴看起来像['control',0,20,40,60,80,100] MWE: 尝试此操作时,我得到一个错误: ValueError:无法将字符串转换为浮点:“控件”Python 将字符串添加到整数的x轴,python,matplotlib,plot,Python,Matplotlib,Plot,我在x轴上画了一个图表(溶液浓度)和效率(y)。我将此设置为在0到100之间为x显示,但我想添加另一个数据点作为控件,而没有任何解决方案。我遇到了一些问题,因为这实际上不适合浓度轴上的任何位置,但我想在0之前或100之后添加它,可能在轴上有一个断点来分隔它们。所以我的x轴看起来像['control',0,20,40,60,80,100] MWE: 尝试此操作时,我得到一个错误: ValueError:无法将字符串转换为浮点:“控件” 你知道我怎么能做这样的工作吗?我查看了XTICK,但这会将x轴
你知道我怎么能做这样的工作吗?我查看了XTICK,但这会将x轴绘制为字符串,因此会失去轴的连续性,这会使绘图变得混乱,因为数据点的间距不是等距的。您可以在图形中添加一个点,作为对
绘图的单独调用,然后调整x轴标签
import matplotlib.pyplot as plt
x_array = [0, 20, 40, 50, 100]
y_array = [2, 3, 4, 5, 6]
x_con = -20
y_con = 1
x_ticks = [-20, 0, 20, 40, 60, 80, 100]
x_labels = ['control', 0, 20, 40, 60, 80, 100]
fig, ax = plt.subplots(1,1)
ax.plot(x_array, y_array)
ax.plot(x_con, y_con, 'ro') # add a single red dot
# set tick positions, adjust label text
ax.xaxis.set_ticks(x_ticks)
ax.xaxis.set_ticklabels(x_labels)
ax.set_xlim(x_con-10, max(x_array)+3)
ax.set_ylim(0,7)
plt.show()
import matplotlib.pyplot as plt
x_array = [0, 20, 40, 50, 100]
y_array = [2, 3, 4, 5, 6]
x_con = -20
y_con = 1
x_ticks = [-20, 0, 20, 40, 60, 80, 100]
x_labels = ['control', 0, 20, 40, 60, 80, 100]
fig, ax = plt.subplots(1,1)
ax.plot(x_array, y_array)
ax.plot(x_con, y_con, 'ro') # add a single red dot
# set tick positions, adjust label text
ax.xaxis.set_ticks(x_ticks)
ax.xaxis.set_ticklabels(x_labels)
ax.set_xlim(x_con-10, max(x_array)+3)
ax.set_ylim(0,7)
plt.show()