0

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

entire dataset plot

head of dataset plot

    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)

plot using answer suggestion

galaxy_d
  • 23
  • 1
  • 9
  • please provide the sample data as **text**, not image – mozway Mar 16 '22 at 14:49
  • 1
    I have edited so you also have the data as text. It is also in the code I have provided – galaxy_d Mar 16 '22 at 15:04
  • @JohanC yes, unfortunately it produces the same graph – galaxy_d Mar 16 '22 at 17:18
  • OK. The default edge color is 'none', so changing the line width won't be visible. Maybe `sns.barplot(..., ec='white', lw=0.5)`? Or `sns.barplot(..., color='skyblue', ec='skyblue', lw=0.5)`? – JohanC Mar 16 '22 at 18:14
  • @JohanC thanks for the suggestion - I would have thought it would work... but strangely the bars are still slightly different widths. I have edited my question to show what I mean – galaxy_d Mar 16 '22 at 19:40
  • When bar widths are close to one pixel, it is unavoidable that small diferences will be visible. You can either try to draw less bars, increase the figure size, or (when saving the plot to a file) increasing the dpi. See e.g. what happens with the letter `m` in this [Wikipedia article about font rasterization](https://en.wikipedia.org/wiki/Font_rasterization) – JohanC Mar 16 '22 at 20:16
  • @JohanC Thank you for the increasing dpi suggestion, it worked for me. Could you please post the answer so I can close the question? – galaxy_d Mar 24 '22 at 17:20

1 Answers1

0

As per the suggestion in the comments, the answer was increasing the dpi when saving the plot

galaxy_d
  • 23
  • 1
  • 9