在matplotlib上使用2 y轴上的散点[Python]

时间:2015-01-17 21:07:03

标签: python matplotlib data-analysis

我正在尝试在matplotlib上绘制2条不同的曲线,但其中一个图需要分散,或者没有连接点的线。反正有没有这样做?现在,我的绘图代码是:

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(epoch, list_of_gas, 'b')
ax2.plot(temp_times, temperatures, 'r')

ax1.set_ylabel('Pressure (torr)', color='b')
ax2.set_ylabel('Temperature (Celcius)', color='r')

ax1.set_title(folder + ' - ' + gas)
ax1.set_xlabel('time (seconds)')
ax1.set_xlim([0, 1000000])
ax2.set_ylim([0,425])
ax1.set_yscale('log')
ax1.set_ylim([ymin,ymax])

plt.show()

但是,我想要ax1.scatter(epoch,list_of_gas,'b')但你不能使用2轴散射。有没有人知道这方面的方法?喜欢删除连接点的线?

1 个答案:

答案 0 :(得分:2)

在双胞胎情况下,你绝对可以拥有scatter图:

import numpy as np
import matplotlib.pyplot as plt

x, y = np.random.random((2,50))
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.scatter(x,y, c='b')
x.sort()
ax2.plot(x, np.sqrt(y)+y, c='r')

此外,如果您想删除连接数据点的行,您可以添加选项:ls='none', or the longer linestyle ='none',如matplotlib documentation中所述。请记住,matplotlib与大多数python库一样,选择了合理的默认值。对于plot的普通电话,默认值为ls='-'以生成连接线。