如何在matplotlib boxplot中为每个x显示两个数字?

时间:2019-04-03 22:04:24

标签: python matplotlib seaborn boxplot

我试图在我的箱线图中将每个x的两个数字彼此相邻显示。但是我的代码使这些数字相互重叠。我无法弄清楚该如何解决,因为我使用的是三个不同的分离数据帧(org_dataholiday_falseholiday_true)。请帮忙。

data.csv:

weekday | holiday | casual | registered
---------------------------------------
   0         1        500       153
   2         0        412       654
   6         1        846       113
   2         0        456       121
   3         0        124       654
  ...       ...       ...       ...
  ...       ...       ...       ...

code:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
plt.style.use('seaborn-notebook')
%matplotlib inline

fig, axes = plt.subplots(figsize=(16, 7))
org_data = pd.read_csv("data.csv")

holiday_false = org_data[(org_data["holiday"] == 0) & (org_data["weekday"] != 0) & (org_data["weekday"] != 6)]
holiday_true = org_data[(org_data["holiday"] == 1) | (org_data["weekday"] == 0) | (org_data["weekday"] == 6)]

ax1 = plt.subplot(121)
sns.boxplot(x=org_data["weekday"], y=holiday_false["casual"], color="orange")
sns.boxplot(x=org_data["weekday"], y=holiday_true["casual"], color="skyblue")
ax1.set_title("Nuber of Casual Users on Holidays and Non-Holidays")
ax1.set_xlabel("Days")
ax1.set_ylabel("Number of Casual Users")

ax2 = plt.subplot(122)
sns.boxplot(x=org_data["weekday"], y=holiday_false["registered"], width=0.50, color="orange")
sns.boxplot(x=org_data["weekday"], y=holiday_true["registered"], width=0.50, color="skyblue")
ax2.set_title("Number of Registered Users on Holidays and Non-Holidays")
ax2.set_xlabel("Days")
ax2.set_ylabel("Number of Registered Users")

plt.show()

the type of chart I get:

enter image description here

the chart I want:

enter image description here

0 个答案:

没有答案