簡體   English   中英

使用matplotlib在2 y軸上使用散點圖

[英]Using scatter on 2 y axis with matplotlib

我正在嘗試在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軸上使用散點圖。 有誰知道解決這個問題的方法嗎? 想刪除連接點的線嗎?

絕對可以在twinx(雙軸,共享x軸)情況下使用散點圖:

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')
ax2.plot(np.sort(x), np.arange(x.size), c='r')

共享x軸的雙軸散點圖

另外,如果要刪除連接數據點的行,則可以添加選項: ls='none'或更長的linestyle='none' ,如matplotlib文檔中所述 請記住,與大多數Python庫一樣,matplotlib選擇了明智的默認值。 對於對plot的普通調用,默認值為ls='-'以產生連接的線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM