[英]Seaborn plot with shared x axis
I am trying to plot 2 columns of a dataframe (one as a bar plot and the other as a scatterplot).我正在尝试 plot dataframe 的 2 列(一个作为条形 plot,另一个作为散点图)。 I can get this working with Matplotlib, but I want it with Seaborn.
我可以使用 Matplotlib 来完成这项工作,但我希望使用 Seaborn。
This is the code:这是代码:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({'Announced Year': [2016, 2017, 2018, 2019],
'Amount Awarded': [12978216, 11582629, 11178338, 11369267],
'Number of Awarded': [18, 14, 13, 13]})
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.scatterplot(x="Announced Year", y="Number of Awarded", data=df, ax=ax2)
sns.barplot(x="Announced Year", y="Amount Awarded", data=df, ax=ax1)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.title('2016 to 2019 Announcements')
You can plot with shared x-axis using x=np.arange(0,len(df))
instead of x="Announced Year"
for scatterplot .您可以使用共享 x 轴 plot 使用
x=np.arange(0,len(df))
而不是x="Announced Year"
作为scatterplot 。
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.barplot(x="Announced Year", y="Amount Awarded", data=df, ax=ax2, alpha=.5)
sns.scatterplot(x=np.arange(0,len(df)), y="Number of Awarded", data=df, ax=ax1)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.title('2016 to 2019 Announcements')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.