这是我的代码。您不需要全部阅读,只需阅读以“ ax2”开头的最后一部分即可。谢谢。
import matplotlib.pyplot as plt
import numpy as np
TMAX = np.array([156, 139, 133, 106, 128, 189, 217, 194, 178, 100, 156, 161, 167, 150, 67, 94, 133, 122, 106, 133, 133, 117, 128, 117, 100, 89, 78, 122, 178, 183, 144, 150, 106, 89, 117, 122, 117, 117, 128, 83, 161, 161, 194, 94, 106, 117, 144, 156, 144, 128, 106, 100, 106, 122, 111, 94, 111, 122, 117, 117, 156, 161, 156, 156, 206, 211, 200, 189, 189, 200, 206, 228, 267, 267, 261, 261, 250, 267, 300, 306, 317, 306, 283, 233, 272, 267, 272, 222, 239, 233, 272, 278, 272, 256, 256, 283, 278, 228, 217, 300, 289, 244, 272, 278, 289, 289, 278, 283, 289, 289, 278, 272, 278, 300, 306, 294, 294, 294, 272, 278, 283, 306, 333, 322, 278, 283, 306, 333, 317, 294, 306, 317, 322, 306, 328, 317, 300, 317, 311, 339, 328, 317, 328, 328, 333, 333, 322, 367, 350, 350, 344, 350, 322, 322, 361, 350, 344, 372, 383, 367, 350, 350, 322, 328, 328, 328, 339, 350, 356, 367, 361, 367, 344, 339, 367, 372, 378, 361, 406, 389, 367, 367, 383, 361, 400, 389, 383, 394, 394, 356, 361, 356, 350, 344, 339, 361, 372, 406, 389, 361, 372, 406, 383, 383, 361, 344, 344, 339, 361, 361, 344, 350, 356, 367, 372, 367, 339, 328, 361, 339, 344, 344, 361, 328, 333, 317, 333, 333, 339, 339, 328, 322, 322, 333, 344, 344, 356, 350, 339, 322, 344, 344, 361, 350, 372, 367, 361, 356, 344, 350, 339, 333, 361, 367, 339, 333, 333, 311, 311, 317, 300, 311, 306, 328, 328, 333, 339, 328, 317, 278, 294, 283, 278, 272, 289, 311, 311, 322, 317, 328, 328, 328, 306, 311, 294, 289, 283, 244, 233, 256, 267, 256, 272, 278, 272, 267, 267, 272, 256, 267, 256, 211, 250, 250, 222, 228, 261, 261, 256, 239, 228, 228, 256, 222, 222, 211, 228, 211, 206, 194, 194, 200, 178, 167, 172, 217, 183, 172, 194, 194, 183, 200, 206, 189, 183, 156, 183, 183, 172, 128, 100, 100, 122, 150, 156, 200, 194, 183, 161, 150, 144, 156, 122, 133, 156, 133, 183, 172, 111, 117, 189, 194, 128, 117, 139])
TMIN = np.array([-160, -267, -267, -261, -155, -266, -306, -294, -278, -256, -200, -193, -250, -266, -272, -294, -294, -289, -300, -239, -260, -277, -250, -267, -243, -238, -239, -294, -278, -233, -194, -211, -211, -238, -261, -282, -261, -233, -228, -210, -256, -255, -288, -272, -239, -260, -294, -272, -222, -260, -343, -322, -172, -267, -272, -217, -216, -288, -272, -267, -182, -244, -239, -221, -255, -222, -167, -144, -156, -122, -144, -222, -206, -106, -139, -178, -161, -100, -106, -133, -122, -133, -150, -139, -156, -156, -122, -111, -122, -83, -83, -78, -89, -83, -72, -94, -83, -89, -67, -83, -67, -72, -50, -67, -83, -117, -89, -28, -39, -44, -72, -50, -56, -71, -50, -66, -39, -39, -50, -49, -33, -28, -44, -61, -33, -17, -28, 0, -17, -17, -11, -38, -60, -28, 22, -33, -10, -27, -16, 0, 22, 6, -22, 0, -17, -10, 0, -11, 0, 33, 22, 44, 33, 17, 33, 11, 28, 17, 61, 67, 50, 28, 33, 50, 28, 39, 56, 61, 61, 44, 44, 83, 50, 39, 67, 78, 56, 61, 100, 78, 61, 61, 44, 72, 67, 50, 67, 78, 50, 94, 94, 89, 89, 39, 56, 72, 78, 83, 72, 72, 83, 50, 72, 89, 89, 61, 89, 100, 61, 50, 44, 67, 72, 78, 83, 72, 72, 67, 89, 89, 83, 67, 89, 67, 78, 50, 39, 56, 72, 44, 72, 61, 61, 78, 39, 72, 50, 67, 50, 56, 78, 67, 33, 44, 56, 67, 67, 28, 44, 44, 39, 50, 22, 44, 39, 17, -6, 6, -10, -6, 11, -10, 11, -5, 0, 11, -21, 0, 6, 33, 6, -21, -11, -32, -17, -6, -27, -22, -11, -6, -61, -56, -44, -61, -33, -56, -44, -61, -33, -55, -55, -61, -39, -32, -56, -56, -50, -50, -50, -61, -50, -72, -82, -61, -71, -99, -89, -93, -78, -78, -67, -82, -61, -44, -77, -94, -111, -94, -94, -78, -122, -144, -128, -122, -182, -172, -138, -150, -156, -122, -111, -133, -121, -106, -132, -133, -100, -122, -155, -183, -194, -200, -189, -172, -167, -210, -178, -161, -166, -228, -222, -194, -161, -167, -194, -200, -200, -167, -167, -156, -138, -166, -150, -144, -150])
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(TMIN, '-o', TMAX, '-o', ms = 0)
plt.legend(['Max in 2005-14', 'Min in 2005-14'])
plt.gca().fill_between(range(len(TMAX)), TMAX, TMIN, facecolor='blue', alpha=0.25)
plt.tick_params(bottom=False, left=False)
plt.ylabel('Temperature (℃)')
plt.title('Record Temperature in Ann Arbor')
plt.margins(x=0)
for spine in plt.gca().spines.values():
spine.set_visible(False)
x = np.array([365/24,365/8,1826/24,2555/24,1095/8,4015/24,4745/24,1825/8,6205/24,6935/24,2555/8,8395/24])
xticks = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
plt.xticks(x, xticks)
y = np.array([-400,-300,-200,-100,0,100,200,300,400])
yticks = ['- 40','- 30','- 20','- 10','0','10','20','30','40']
plt.yticks(y, yticks)
ax2 = ax.twinx()
ax2.spines['top'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.set_yticks([-400,-344,-288,-233,-177,-122,-66,-11,44,100,155,211,266,322,377,400])
ax2.set_yticklabels(['- 40','- 30','- 20','- 10','0','10','20','30','40','50','60','70','80','90','100',''])
plt.show()
然后是情节:
。
我使用twinx()
在右侧创建一个新轴。但是,您可以看到在右上角有一个无用的勾。这是因为刻度线必须在顶部和底部开始并结束。那么如何删除轴上的某个刻度?