Skip to content

Instantly share code, notes, and snippets.

@linzino7
Created January 15, 2019 09:13
Show Gist options
  • Save linzino7/9c9fa2c18f864dc6062e4bdeceba30fd to your computer and use it in GitHub Desktop.
Save linzino7/9c9fa2c18f864dc6062e4bdeceba30fd to your computer and use it in GitHub Desktop.
X axis in matplotlib with different scales
# 將不同資料長度畫在不同的為度上
# ref https://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html
import matplotlib.pyplot as plt
x_values1=[1,2,3,4,5]
y_values1=[1,2,2,4,1]
x_values2=[-1000,-800,-600,-400,-200,-100,0]
y_values2=[10,20,39,40,50,60,10]
x_values3=[150,200,250,300,350]
y_values3=[10,20,30,40,50]
fig=plt.figure()
ax=fig.add_subplot(111, label="1")
ax2=fig.add_subplot(111, label="2", frame_on=False)
ax3=fig.add_subplot(111, label="3", frame_on=False)
ax.plot(x_values1, y_values1, color="C0")
ax.set_xlabel("x label 1", color="C0")
ax.set_ylabel("y label 1", color="C0")
ax.tick_params(axis='x', colors="C0")
ax.tick_params(axis='y', colors="C0")
ax2.scatter(x_values2, y_values2, color="C1")
ax2.xaxis.tick_top()
ax2.yaxis.tick_right()
ax2.set_xlabel('x label 2', color="C1")
ax2.set_ylabel('y label 2', color="C1")
ax2.xaxis.set_label_position('top')
ax2.yaxis.set_label_position('right')
ax2.tick_params(axis='x', colors="C1")
ax2.tick_params(axis='y', colors="C1")
ax3.plot(x_values3, y_values3, color="C3")
ax3.set_xticks([])
ax3.set_yticks([])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment