注册 登录
编程论坛 Python论坛

新手关于双Y轴直方图与折线图结合的问题请教!!

zys0903 发布于 2018-08-27 16:27, 1636 次点击
我是Python新手,现遇到如下问题,向论坛中的高手们请教!
有如下三列数据,想以X为横轴,Col1为第一纵轴做直方图,以Col2为第二Y轴做折线图。
X    Col1    Col2
2000    0.9291    100
4000    0.9585    75
6000    0.9672    10
10000    0.9839    10
27370    0.992    5
50000    0.9663    25
100000    0.9526    25
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录


通过第一种方式,X轴的数字显示不全,而第二种方式折线又被覆盖了。请问如何保证X轴、直方图以及折线图均显示正常?
代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt  
import seaborn as sns

sns.set_style("whitegrid")
df=pd.read_csv('data.csv')

ax1=sns.barplot(x = df['X'],y = df['Col1'])
plt.ylim(0.92,1.00)

p = df['Col2']  
ax2 = ax1.twinx()
ax2=p.plot(color='b',style='--o',linewidth=2)
plt.ylabel('Col2')
plt.show()

##第2种方式
col2 = df['Col2']  
ax1=col2.plot(color='b',style='--o',linewidth=2)
plt.ylabel('Col2')

x=[-1,0,1,2,3,4,5,6]
for a,b in zip(x,p):
    plt.text(a+1, b+1, '%.f' % b, ha='center', va= 'bottom',fontsize=12)
ax2 = ax1.twinx()
ax2=sns.barplot(x = df['X'],y = df['Col1'])
plt.ylim(0.92,1.00)

plt.show()
1 回复
#2
huangyg1232018-09-12 17:55
1