2016-06-09 16 views
5

Tôi có hai đồ thị cho cả hai đều có cùng trục x, nhưng với các tỷ lệ trục y khác nhau.Hợp nhất các điểm con matplotlib với trục x chia sẻ

Cốt truyện có trục thông thường là dữ liệu có đường xu hướng mô tả phân rã trong khi tỷ lệ bán nhật ký y mô tả độ chính xác của phù hợp.

fig1 = plt.figure(figsize=(15,6)) 
ax1 = fig1.add_subplot(111) 

# Plot of the decay model 
ax1.plot(FreqTime1,DecayCount1, '.', color='mediumaquamarine') 

# Plot of the optimized fit 
ax1.plot(x1, y1M, '-k', label='Fitting Function: $f(t) = %.3f e^{%.3f\t} \ 
     %+.3f$' % (aR1,kR1,bR1)) 

ax1.set_xlabel('Time (sec)') 
ax1.set_ylabel('Count') 
ax1.set_title('Run 1 of Cesium-137 Decay') 

# Allows me to change scales 
# ax1.set_yscale('log') 
ax1.legend(bbox_to_anchor=(1.0, 1.0), prop={'size':15}, fancybox=True, shadow=True) 

enter image description here enter image description here

Bây giờ, tôi đang cố gắng tìm ra để thực hiện cả hai gần nhau như các ví dụ được cung cấp bởi liên kết này http://matplotlib.org/examples/pylab_examples/subplots_demo.html

Đặc biệt, điều này một

enter image description here

Khi nhìn vào đoạn code ví dụ như, tôi là một chút nhầm lẫn về cách cấy ghép 3 điều:

1) Mở rộng quy mô các trục khác nhau

2) Giữ kích thước con số tương tự cho đồ thị phân rã theo cấp số nhân nhưng có biểu đồ đường có kích thước y nhỏ hơn và cùng kích thước x.

Ví dụ:

enter image description here

3) Giữ nhãn của các chức năng để xuất hiện trong chỉ duy nhất biểu đồ phân rã.

Mọi trợ giúp sẽ được đánh giá cao nhất.

Trả lời

11

Nhìn vào mã và ý kiến ​​trong đó:

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib import gridspec 

# Simple data to display in various forms 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

fig = plt.figure() 
# set height ratios for sublots 
gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1]) 

# the fisrt subplot 
ax0 = plt.subplot(gs[0]) 
# log scale for axis Y of the first subplot 
ax0.set_yscale("log") 
line0, = ax0.plot(x, y, color='r') 

#the second subplot 
# shared axis X 
ax1 = plt.subplot(gs[1], sharex = ax0) 
line1, = ax1.plot(x, y, color='b', linestyle='--') 
plt.setp(ax0.get_xticklabels(), visible=False) 
# remove last tick label for the second subplot 
yticks = ax1.yaxis.get_major_ticks() 
yticks[-1].label1.set_visible(False) 

# put lened on first subplot 
ax0.legend((line0, line1), ('red line', 'blue line'), loc='lower left') 

# remove vertical gap between subplots 
plt.subplots_adjust(hspace=.0) 
plt.show() 

enter image description here

+0

Brilliant thưa ông. Cảm ơn bạn. – DarthLazar

+0

@Serenity Câu trả lời hay! Nhưng có thể chia sẻ trục x nếu tôi sử dụng '' fig, axis = plt.subplots (nrows = 4) '' để tạo mảng trục của tôi không? –

+0

Vâng, có thể, tại sao không? – Serenity

Các vấn đề liên quan