seaborn 条形图中的条宽问题
Issue with widths of bars in seaborn barplot
我正在 Python 中绘制一个 seaborn 条形图,但是,当我绘制整个数据集时,条形的宽度不同。当我只绘制数据集的头部时,我没有问题。如何解决?非常感谢任何建议!
绘制整个数据集的代码,仅绘制头部,并输出数据帧的头部:
import numpy as np
import pandas as pd
import math
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.dates as md
import matplotlib.ticker as ticker
from datetime import datetime, timedelta
# create dataframes that will be used
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(310), freq='D')
np.random.seed(seed=1111)
data_a = np.random.randint(-20, high=30, size=len(days))
dataframe = pd.DataFrame({'date': days, 'a': data_a})
dataframe = dataframe.set_index('date')
dataframe_date = dataframe.copy()
dataframe_date = dataframe_date.reset_index()
dataframe_date['date'] = dataframe_date['date'].dt.date
dataframe_date_head = dataframe_date.head(20)
# plot whole dataframe
fig = plt.figure()
ax = plt.axes()
b_plot = sns.barplot(data = dataframe_date, x=dataframe_date['date'], y=dataframe_date['a'], ax=ax)
ax.xaxis.set_major_locator(ticker.AutoLocator())
ax.margins(x=0)
plt.xticks(rotation=70)
ax.set_xticks(np.arange(len(dataframe_date)))
ax.set_xticklabels(dataframe_date.date.apply(lambda x: str(x.day) + '-' + str(x.month) + '-' + str(x.year)))
ax.xaxis.set_major_locator(ticker.AutoLocator())
plt.show()
# plot only head(20) of the dataframe
fig = plt.figure()
ax1 = plt.axes()
b_plot = sns.barplot(data = dataframe_date_head, x=dataframe_date_head['date'], y=dataframe_date_head['a'], ax=ax1)
ax1.xaxis.set_major_locator(ticker.AutoLocator())
ax1.margins(x=0)
plt.xticks(rotation=70)
ax1.set_xticks(np.arange(len(dataframe_date_head)))
ax1.set_xticklabels(dataframe_date_head.date.apply(lambda x: str(x.day) + '-' + str(x.month) + '-' + str(x.year)))
ax1.xaxis.set_major_locator(ticker.AutoLocator())
plt.show()
# print head of the dataframe
dataframe_date_head
date a
0 2022-03-16 8
1 2022-03-17 17
2 2022-03-18 -3
3 2022-03-19 -8
4 2022-03-20 14
5 2022-03-21 4
6 2022-03-22 2
7 2022-03-23 0
8 2022-03-24 -9
9 2022-03-25 -6
10 2022-03-26 -12
11 2022-03-27 18
12 2022-03-28 -8
13 2022-03-29 26
14 2022-03-30 2
15 2022-03-31 -12
16 2022-04-01 21
17 2022-04-02 22
18 2022-04-03 -8
19 2022-04-04 10
编辑:我认为这与我的环境有关(我在 Microsoft edge 上使用 Jupyter Notebook)
这里是使用为 bar_plot 建议的以下代码的输出:
b_plot = sns.barplot(data = dataframe_date, x=dataframe_date['date'], y=dataframe_date['a'], ax=ax, color = 'blue', ec='blue', lw=0.5)
根据评论中的建议,答案是在保存绘图时增加 dpi
我正在 Python 中绘制一个 seaborn 条形图,但是,当我绘制整个数据集时,条形的宽度不同。当我只绘制数据集的头部时,我没有问题。如何解决?非常感谢任何建议!
绘制整个数据集的代码,仅绘制头部,并输出数据帧的头部:
import numpy as np
import pandas as pd
import math
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.dates as md
import matplotlib.ticker as ticker
from datetime import datetime, timedelta
# create dataframes that will be used
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(310), freq='D')
np.random.seed(seed=1111)
data_a = np.random.randint(-20, high=30, size=len(days))
dataframe = pd.DataFrame({'date': days, 'a': data_a})
dataframe = dataframe.set_index('date')
dataframe_date = dataframe.copy()
dataframe_date = dataframe_date.reset_index()
dataframe_date['date'] = dataframe_date['date'].dt.date
dataframe_date_head = dataframe_date.head(20)
# plot whole dataframe
fig = plt.figure()
ax = plt.axes()
b_plot = sns.barplot(data = dataframe_date, x=dataframe_date['date'], y=dataframe_date['a'], ax=ax)
ax.xaxis.set_major_locator(ticker.AutoLocator())
ax.margins(x=0)
plt.xticks(rotation=70)
ax.set_xticks(np.arange(len(dataframe_date)))
ax.set_xticklabels(dataframe_date.date.apply(lambda x: str(x.day) + '-' + str(x.month) + '-' + str(x.year)))
ax.xaxis.set_major_locator(ticker.AutoLocator())
plt.show()
# plot only head(20) of the dataframe
fig = plt.figure()
ax1 = plt.axes()
b_plot = sns.barplot(data = dataframe_date_head, x=dataframe_date_head['date'], y=dataframe_date_head['a'], ax=ax1)
ax1.xaxis.set_major_locator(ticker.AutoLocator())
ax1.margins(x=0)
plt.xticks(rotation=70)
ax1.set_xticks(np.arange(len(dataframe_date_head)))
ax1.set_xticklabels(dataframe_date_head.date.apply(lambda x: str(x.day) + '-' + str(x.month) + '-' + str(x.year)))
ax1.xaxis.set_major_locator(ticker.AutoLocator())
plt.show()
# print head of the dataframe
dataframe_date_head
date a
0 2022-03-16 8
1 2022-03-17 17
2 2022-03-18 -3
3 2022-03-19 -8
4 2022-03-20 14
5 2022-03-21 4
6 2022-03-22 2
7 2022-03-23 0
8 2022-03-24 -9
9 2022-03-25 -6
10 2022-03-26 -12
11 2022-03-27 18
12 2022-03-28 -8
13 2022-03-29 26
14 2022-03-30 2
15 2022-03-31 -12
16 2022-04-01 21
17 2022-04-02 22
18 2022-04-03 -8
19 2022-04-04 10
编辑:我认为这与我的环境有关(我在 Microsoft edge 上使用 Jupyter Notebook)
这里是使用为 bar_plot 建议的以下代码的输出:
b_plot = sns.barplot(data = dataframe_date, x=dataframe_date['date'], y=dataframe_date['a'], ax=ax, color = 'blue', ec='blue', lw=0.5)
根据评论中的建议,答案是在保存绘图时增加 dpi