Sharex有3个子图,多个y

时间:2018-04-20 10:37:16

标签: python python-2.7 matplotlib graph

基本上,我想将3个图堆叠在一起,以便它们可以共享x。问题是,有些图使用了twinx命令(?),因为它们有2个或更多y轴。

如果我不清楚我的意思,我已经手动将图表放在一起看看它看起来如何,但它并不那么好。 (注意/忽略图表中的行,我也不喜欢1或2上的x标签)

Example of graphs

此外,下图显示了其中一个数字代码的示例 - 如果它有帮助的话。

Sub Unik()
Dim sh As Worksheet, sh2 As Worksheet, lr As Long, rng As Range
Set sh = Sheet1 'Edit sheet name
Dim strFile_Path As String
Dim i As Integer
 Dim iCntr As Long
 Close #1
    Open "C:\Users\Downloads\ghds.txt" For Output As #1
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh.Range("A2:A" & lr)
rng.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
sh.Rows(1).Hidden = True
For Each c In rng.SpecialCells(xlCellTypeVisible)
If Mystr = "" Then
Mystr = c.Value
Else
Mystr = Mystr & ", " & c.Value
Print #1, c.Value
End If
Next
Close #1
End Sub

1 个答案:

答案 0 :(得分:0)

这是一个简单的脚本,可以执行您想要的操作。它使用plt.subplots创建具有共享x轴的前三个轴,然后使用ax.twinx创建双轴。

其中一个共享轴上的偏移脊柱是使用this example on the matplotlib gallery完成的。

import matplotlib.pyplot as plt

def make_patch_spines_invisible(ax, offset):
    '''
    This function inspired by this example, to offset a twin axis spine. 
    link: https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html
    '''
    ax.spines["right"].set_position(("axes", offset))
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)
    ax.spines["right"].set_visible(True)

# First create your figure, with 3 subplots sharing the x axis.
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, sharex=True)

# make some room on the right for the twin axes
fig.subplots_adjust(right=0.8)

# The easy ones, create the first twin axes on ax1 and ax2
ax1t = ax1.twinx()
ax2t = ax2.twinx()

# Create the twin axis on ax2 with an offset spine
ax2tb = ax2.twinx()
make_patch_spines_invisible(ax2tb, offset=1.15)

# Here I just set some axes limits to show the y axis scales are all independent
ax3.set_xlim(0, 900)

ax1.set_ylim(0, 2000)
ax2.set_ylim(0, 1000)
ax3.set_ylim(0, 50)

ax1t.set_ylim(0, 12)
ax2t.set_ylim(0, 12)
ax2tb.set_ylim(0, 100)

plt.show()

enter image description here