Seaborn 线图和条形图在 X 轴上不对齐 [英] Seaborn lineplot and barplot don't align in the X axis

查看:232
本文介绍了Seaborn 线图和条形图在 X 轴上不对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,过去 5 个小时我一直被困在这里,但我似乎无法正确绘制此组合图.

将pandas导入为pd从 matplotlib 导入 pyplot 作为 plt将 seaborn 作为 sns 导入data = pd.read_csv('rating_conversion.csv')df = pd.DataFrame(数据)total_conversion_rate = df['overall_conversion_rate']page_view_conversion = df['page_view_conversion']Avg_Rating = df['avg_rating']Total_Hired = df['total_hires']df[:12]图, ax1 = plt.subplots(figsize=(10,6))颜色 = '标签:绿色'ax1.set_title('每个评级组的雇佣总数和平均转化率 %', fontsize=16)ax1.set_xlabel('Average_Rating', fontsize=16)ax1.set_ylabel('Total_Hired', fontsize=16, color=color)ax2 = sns.barplot(x=Avg_Rating,y=Total_Hired,数据=df,palette='summer_r')ax1.tick_params(axis='y')ax2 = ax1.twinx()颜色 = '标签:红色'ax2.set_ylabel('Avg Conversion %', fontsize=16, color=color)ax2 = sns.lineplot(x=Avg_Rating, y=overall_conversion_rate, data=df, sort=False, color=color)ax2.tick_params(axis='y', color=color)plt.show()

我的期望是这样的,平均转化率百分比和雇佣总数共享相同的 X 轴.

使用pointplot:

fig, ax = plt.subplots()ax_twin = ax.twinx()sns.barplot(x=day", y=total_bill", data=tips, ax=ax)sns.pointplot(x='day', y='total_bill', data=tips, ax=ax_twin)

使用lineplotrank

tips['rank'] = tips['size'].rank(method='dense') - 1图, ax = plt.subplots()ax_twin = ax.twinx()sns.barplot(x='size', y='total_bill', data=tips, ax=ax)sns.lineplot(x='rank', y='total_bill', data=tips, err_style='bars', ax=ax_twin)

Okay so I've been stuck here for the past 5 hours but I can't seem to do this combo graph correctly.

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns


data = pd.read_csv('rating_conversion.csv')
df = pd.DataFrame(data)
overall_conversion_rate = df['overall_conversion_rate']
page_view_conversion = df['page_view_conversion']
Avg_Rating = df['avg_rating']
Total_Hired = df['total_hires']
df[:12]

fig, ax1 = plt.subplots(figsize=(10,6))
color = 'tab:green'
ax1.set_title('Total Hired and Avg Conversion % Per Rating Group', fontsize=16)
ax1.set_xlabel('Average_Rating', fontsize=16)
ax1.set_ylabel('Total_Hired', fontsize=16, color=color)
ax2 = sns.barplot(x=Avg_Rating, y=Total_Hired, data=df, palette='summer_r')
ax1.tick_params(axis='y')
ax2 = ax1.twinx()
color = 'tab:red'
ax2.set_ylabel('Avg Conversion %', fontsize=16, color=color)
ax2 = sns.lineplot(x=Avg_Rating, y=overall_conversion_rate, data=df, sort=False, color=color)
ax2.tick_params(axis='y', color=color)
plt.show()

My expectation is something like this where the Average conversion % and Total Hired share the same X axis.

enter image description here

Please help. Here's the code that I've been using as my example:

#Libraries
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

#Data
#create list of months
Month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June',
'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
#create list for made up average temperatures
Avg_Temp = [35, 45, 55, 65, 75, 85, 95, 100, 85, 65, 45, 35]
#create list for made up average precipitation %
Avg_Precipitation_Perc = [.90, .75, .55, .10, .35, .05, .05, .08, .20, .45, .65, .80]
#assign lists to a value
data = {'Month': Month, 'Avg_Temp': Avg_Temp, 'Avg_Precipitation _Perc': Avg_Precipitation_Perc}
#convert dictionary to a dataframe
df = pd.DataFrame(data)
#Print out all rows
df[:12]


#Create combo chart
fig, ax1 = plt.subplots(figsize=(10,6))
color = 'tab:green'
ax1.set_title('Average Precipitation Percentage by Month', fontsize=16)
ax1.set_xlabel('Month', fontsize=16)
ax1.set_ylabel('Avg Temp', fontsize=16, color=color)
ax2 = sns.barplot(x='Month', y='Avg_Temp', data = df, palette='summer')
ax1.tick_params(axis='y')
ax2 = ax1.twinx()
color = 'tab:red'
ax2.set_ylabel('Avg Precipitation %', fontsize=16, color=color)
ax2 = sns.lineplot(x='Month', y='Avg_Precipitation _Perc', data = df, sort=False, color=color)
ax2.tick_params(axis='y', color=color)
plt.show()

This is the content of rating_conversion.csv https://paste.ubuntu.com/p/8w63wP2z9J/

解决方案

The issue is that barplot doesn't plot at your actual x values. It treats the x variable as categorical so the locations are at x = 0, 1, 2, etc. lineplot, on the other hand, uses the actual x value. You'll notice in your figure that the line plot is between the 5th and 6th bars--corresponding to x locations between x = 4 and x = 5.

A workaround is to use pointplot instead of lineplot. pointplot treats the x variable as categorical, similar to barplot.

You can still use lineplot if you recalculate your x values so they start at 0 and go up by 1. An easy way to do this is to use the rank method. This would be easier than changing barplot to plot at the actual x values.

As an aside, you aren't quite using twinx properly. It works but you aren't utilizing ax2. I'll illustrate in the example.

The issue

import seaborn as sns
tips = sns.load_dataset("tips")

fig, ax = plt.subplots()
ax_twin = ax.twinx()
sns.barplot(x="day", y="total_bill", data=tips, ax=ax)
sns.lineplot(x='day', y='total_bill', data=tips, err_style='bars', ax=ax_twin)

Using pointplot:

fig, ax = plt.subplots()
ax_twin = ax.twinx()
sns.barplot(x="day", y="total_bill", data=tips, ax=ax)
sns.pointplot(x='day', y='total_bill', data=tips, ax=ax_twin)

Using lineplot and rank

tips['rank'] = tips['size'].rank(method='dense') - 1

fig, ax = plt.subplots()
ax_twin = ax.twinx()
sns.barplot(x='size', y='total_bill', data=tips, ax=ax)
sns.lineplot(x='rank', y='total_bill', data=tips, err_style='bars', ax=ax_twin)

这篇关于Seaborn 线图和条形图在 X 轴上不对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆