-5
我想用python制作一个这样的图。我用excel画了这个图。如何制作一个有两个x轴的图形?
我想用python制作一个这样的图。我用excel画了这个图。如何制作一个有两个x轴的图形?
你将不得不建立一些像两个地块,并使用x轴的其中之一是这个二次轴。您的情节中还有其他功能,比如设置蜱和他们的位置的标签。下面的示例播放有点用这些元素,所以你应该能够适应您的问题:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
# Add some extra space for the second axis at the bottom
fig.subplots_adjust(bottom=0.2)
ax1.set_xticks([1,2,4,5,7,8])
ax1.set_xlim(0,9)
ax1.set_xticklabels(('2015','2016','2015','2016','2015','2016'))
ax2.spines["bottom"].set_position(("axes", -0.15))
ax2.xaxis.set_ticks_position("bottom")
ax2.spines["bottom"].set_visible(True)
ax2.set_xticks([1.5,4.5,7.5])
ax2.set_xticklabels(('1','2','3'))
ax2.set_xlim(0,9)
b1 = np.random.randint(0,100,6)
b2 = np.random.randint(0,100,6)
b3 = np.random.randint(0,100,6)
plt.bar(np.array([1,2,4,5,7,8])-0.4,b1,color='blue')
plt.bar(np.array([1,2,4,5,7,8])-0.4,b2,color='orange',bottom=b1)
plt.bar(np.array([1,2,4,5,7,8])-0.4,b3,color='yellow',bottom=b1+b2)
plt.show()
,结果是这样的: