使用matplotlib绘制折线和列的混合图形,但不显示折线

时间:2020-10-29 10:06:25

标签: python matplotlib

这是我编写的代码。可以显示图例,但不能显示虚线。
结果如下图所示。
如何在直方图中显示虚线,并且还可以显示每个点的值?
如何详细实现此图像?
如何使X轴值更大?

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

config = {
"font.family":'serif',
"font.size": 10,
"mathtext.fontset":'stix',
"font.serif": ['SimSun'],
}
rcParams.update(config)
plt.rc('font',family='Times New Roman')


font1 = {'family' : 'Times New Roman',
        'weight' : 'normal',
        'size'   : 17,
        }

font2 = {'family' : 'Times New Roman',
        'weight' : 'normal',
        'size'   : 11,
         }
font3 = {'family' : 'Times New Roman',
        'weight' : 'normal',
        'size'   : 19,
        }
font4 = {'family' : 'Times New Roman',
        'weight' : 'normal',
        'size'   : 22,
        }

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.set_ylim(0, 3)
ax2.set_ylim(95, 130)

ax1.set_ylabel('$t_{score}$',font3)
ax2.set_ylabel('$node_iNum$',font3)

node = [110,118,121,107,123]
x = np.arange(5)
bar_wigth = 0.45

x_list= ['$node_1$','$node_2$','$node_3$','$node_4$','$node_5$']
ax2.bar(x,node,bar_wigth,color='green',label='$node_iNum$',alpha=0.6, tick_label=x_list)
ax2.set_xticklabels(x_list,font3)

t_score=[0.95,0.47,0.79,1.42,1.11]
quezhi=[1.94,1.94,1.94,1.94,1.94]
l=[i for i in range(5)]

ax2.plot(l, t_score,'r-',label='$t_{th}$')
ax2.plot(l,quezhi,'b--',label='$t_{score}$')
ax2.plot(l,t_score,'ro',l,quezhi,'b^')

for a, b in zip(l, t_score):
    ax2.text(a, b, b, ha='center', va='bottom', fontsize=10)
for a, b in zip(l, quezhi):
    ax2.text(a, b, b, ha='center', va='bottom', fontsize=10)
ax2.legend(loc='upper left', prop=font2)
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

@@先生。 T评论说,您似乎在混淆第一轴和第二轴。如果第二个轴是条形图,那么剩下的时间就是第一个轴,不是吗?

img

enter image description here