python - 组合 Seaborn 地块没有正确排列
解决方案
您需要使用 matplotlib 中的 twinx() 并且您的第一个图需要只是 matplotlib,而不是 seaborn。我不确定为什么 seaborn 对组合图有问题,但我遇到了和你一样的问题。这是我的代码,其中包含来自 kaggle 的人口数据:
#Create bar plot for annual growth by year
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
#import dataframe for data
df = pd.read_csv('df.csv')
#Create combo chart
fig, ax1 = plt.subplots(figsize=(10,6))
color = 'tab:green'
#bar plot creation
ax1.bar(df['Year'],df['Population Growth'],color='y')
#specify we want to share the same x-axis
ax2 = ax1.twinx()
#lineplot creation
ax2 = sns.lineplot(x='Year', y='Percent Growth', data=df,color='#C33E3E')
plt.show()
使用此代码,我得到以下图表:
推荐阅读
- intellij-idea - 如何在 JetBrains DataGrip 中显示列和表注释?
- javascript - 带有 -F 选项的 cURL 使用 Axios 进入 NodeJS
- r - 为什么我的股票分析总是得到 ARIMA (0,0,0)?
- python - 将十六进制输出转换为最多 53 位(0 或 1 个值)的二进制
- tiddlywiki - 在 TiddlyWiki 5 中标记导入的 BibTex 条目
- c# - 何时判断连续的零星浮点流是否与整数相交?
- php - Doctrine querybuilder 不适用于我在 PHP 中的 SQL 语句
- sql - Oracle SQL 模拟嵌套窗口函数
- javascript - 页面重新加载后按下按钮时 POST 请求触发两次
- javascript - 如何在 Vue-Toasted 中添加 css?