matplotlib与twinx子图不共享蜱

时间:2018-01-15 03:02:20

标签: python matplotlib

我几天前刚开始玩matplotlib,但我终于将四个数据流放到了图表上。我目前的问题是我希望条形图中的x网格线向上移动,或者至少相同。

非常好,我错过了什么?

谢谢,

enter image description here

import numpy as np
import matplotlib.pyplot as plt

ag=[7863440.695,5838684.272,5761731.015,5174288.99,5168869.827,4726030.716,4475078.584,4350399.139,4167613.258,3926692.051,3910475.441,3810358.137,3228663.543,2874138.57,2840444.647,2662246.952,2636947.758,2623206.986,2286573.796,2229170.832]
bg=[7869940.695,5839984.272,5769931.015,5179988.99,5169969.827,4726990.716,4499078.584,4359999.139,4199613.258,3926992.051,3999475.441,3899358.137,3299963.543,2899938.57,2849944.647,2699246.952,2636997.758,2993206.986,2999573.796,2229999.832]
am=[38.16734988,28.24613415,36.72978414,27.19943414,35.67380739,32.94107014,34.76804705,34.73393072,36.49127507,36.65402418,33.61116445,35.31467108,32.69152651,35.43487224,36.53573547,36.18453721,33.84686164,36.67667917,36.60780944,37.44863426]
bm=[38.26734988,28.99913415,36.99978414,27.99943414,34.67380739,30.94107014,32.76804705,32.73393072,34.49127507,37.65402418,34.61116445,39.31467108,32.99952651,34.43487224,36.53573547,36.1849953721,33.894686164,36.697667917,36.960780944,37.94863426]

plt.subplot(212)
n_groups=20
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8

rects1 = plt.bar(index, ag, bar_width,
    alpha=opacity,
    color='b',
    label='This is blue')

rects2 = plt.bar(index + bar_width, bg, bar_width,
    alpha=opacity,
    color='orange',
    label='This is orange')

plt.xlabel('')
plt.ylabel('Bottom Part',fontsize=11)
plt.title('')
plt.tick_params(labelsize=8)
plt.xticks(index + bar_width, ('A', 'B', 'C', 'D','E','F','G','H','I','J','a','b','c','d','e','f','g','h','i','j'),fontsize=11,rotation='vertical')

#This is for the lines at the top of the graph
line1=plt.subplot(511)
line2=line1.twinx()
line1.set_title('')
line1.set_ylabel('Top Part',fontsize=11)

line1.tick_params(labelsize=11)
line1.plot(index,am,color='blue')
line2.plot(index,bm,color='orange')
line2.grid(None)
line2.tick_params(direction='out', length=6, width=2,         colors='lightgray',labelsize=1)

plt.suptitle('Atleast I finally learned the supertitle', fontsize=16)
plt.legend()
plt.subplots_adjust(left=0.1, wspace=0.8, top=0.8, bottom=0.13)
plt.show()

1 个答案:

答案 0 :(得分:0)

你可能想要这样的东西:

import numpy as np
import matplotlib.pyplot as plt

ag = [7863440.695,5838684.272,5761731.015,5174288.99,5168869.827,4726030.716,4475078.584,4350399.139,4167613.258,3926692.051,3910475.441,3810358.137,3228663.543,2874138.57,2840444.647,2662246.952,2636947.758,2623206.986,2286573.796,2229170.832]
bg = [7869940.695,5839984.272,5769931.015,5179988.99,5169969.827,4726990.716,4499078.584,4359999.139,4199613.258,3926992.051,3999475.441,3899358.137,3299963.543,2899938.57,2849944.647,2699246.952,2636997.758,2993206.986,2999573.796,2229999.832]
am = [38.16734988,28.24613415,36.72978414,27.19943414,35.67380739,32.94107014,34.76804705,34.73393072,36.49127507,36.65402418,33.61116445,35.31467108,32.69152651,35.43487224,36.53573547,36.18453721,33.84686164,36.67667917,36.60780944,37.44863426]
bm = [38.26734988,28.99913415,36.99978414,27.99943414,34.67380739,30.94107014,32.76804705,32.73393072,34.49127507,37.65402418,34.61116445,39.31467108,32.99952651,34.43487224,36.53573547,36.1849953721,33.894686164,36.697667917,36.960780944,37.94863426]

n_groups = 20
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8

fig = plt.figure()
ax = fig.add_subplot( 2, 1, 2 )
bx = fig.add_subplot( 5, 1, 1 ) 


rects1 = ax.bar(index - bar_width, ag, bar_width,
    alpha=opacity,
    color='b',
    label='This is blue')

rects2 = ax.bar(index , bg, bar_width,
    alpha=opacity,
    color='orange',
    label='This is orange')

ax.set_ylabel('Bottom Part',fontsize=11)
ax.set_xticks( index  )
bx.set_xticks( index  )
ax.set_xticklabels( ('A', 'B', 'C', 'D','E','F','G','H','I','J','a','b','c','d','e','f','g','h','i','j'),fontdict={'fontsize':11,'rotation':'vertical'})

bx.set_xticklabels( [ (lambda x: str(x) if not x % 5 else '')(x) for x in index] ,fontdict={'fontsize':11})

bx.set_ylabel('Top Part',fontsize=11)

bx.plot(index,am,color='blue')
bx.plot(index,bm,color='orange')

for xpos in index:
    ax.axvline(xpos,0,2, clip_on=False,zorder=-100, color='gray',linewidth=1)
ax.set_xlim([-1,20])
bx.set_xlim([-1,20])
ax.grid()
bx.grid()
bx.tick_params(bottom='off', labelbottom='off',top='on', labeltop='on', which='both')
bx.tick_params(axis='y', labelsize=7 )


plt.suptitle('Atleast I finally learned the supertitle', fontsize=16)
plt.legend()
fig.subplots_adjust(left=0.1, wspace=0.8, top=0.8, bottom=0.13)
plt.show()

enter image description here

我使用不同的风格,并没有费心去除框架等,但我想你会设法得到你想要的外观。我也玩bx.add_subplot(..., sharex=ax)位,这导致相同的标签。请注意,我没有移动橙色条和右边的刻度线,而是将蓝色条向左移动。其余的只是设置正确的绘图范围的问题。