I am plotting a seaborn barplot in Python, however, the widths of the bars are different when I plot the entire dataset. When I only plot the head of the dataset, I do not have a problem. How can this be solved? Would appreciate any advice!
Code for plotting the entire dataset, only the head, and outputting the head of the dataframe:
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
Edit: I think it is something to do with my environment (I am using Jupyter Notebook on Microsoft edge)
Here is the output using the following code suggested for the 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)
As per the suggestion in the comments, the answer was increasing the dpi when saving the plot