4

I'm trying to create a histogram with seaborn, where the bins start at 0 and go to 1. However, there is only date in the range from 0.22 to 0.34. I want the empty space more for a visual effect to better present the data.

I create my sheet with

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg', 'pdf')

df = pd.read_excel('test.xlsx', sheetname='IvT')

Here I create a variable for my list and one that I think should define the range of the bins of the histogram.

st = pd.Series(df['Short total'])
a = np.arange(0, 1, 15, dtype=None)

And the histogram itself looks like this

sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Ration short/total', fontsize=18)
plt.title ('CO3 In vitro transcription, Na+', fontsize=22)

ax = sns.distplot(st, bins=a, kde=False)

plt.savefig("hist.svg", format="svg")
plt.show()

Histogram

It creates a graph bit the range in x goes from 0 to 0.2050 and in y from -0.04 to 0.04. So completely different from what I expect. I google searched for quite some time but can't seem to find an answer to my specific problem.

Already, thanks for your help guys.

Jul
  • 173
  • 3
  • 3
  • 7

1 Answers1

5

There are a few approaches to achieve the desired results here. For example, you can change the xaxis limits after you have plotted the histogram, or adjust the range over which the bins are created.

import seaborn as sns

# Load sample data and create a column with values in the suitable range
iris = sns.load_dataset('iris')
iris['norm_sep_len'] = iris['sepal_length'] / (iris['sepal_length'].max()*2)
sns.distplot(iris['norm_sep_len'], bins=10, kde=False)

enter image description here

Change the xaxis limits (the bins are still created over the range of your data):

ax = sns.distplot(iris['norm_sep_len'], bins=10, kde=False)
ax.set_xlim(0,1)

enter image description here

Create the bins over the range 0 to 1:

sns.distplot(iris['norm_sep_len'], bins=10, kde=False, hist_kws={'range':(0,1)})

enter image description here

Since the range for the bins is larger, you now need to use more bins if you want to have the same bin width as when adjusting the xlim:

sns.distplot(iris['norm_sep_len'], bins=45, kde=False, hist_kws={'range':(0,1)})

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159
  • Thank you very much. That did the trick. Is there a way, I also could use the following argument to get borders around the bars? hist_kws=dict(edgecolor="k", linewidth=2) – Jul Oct 29 '17 at 13:20
  • 1
    @Jul `hist_kws` sends the arguments to the underlying histogram function from matplotlib. You can view all the arguments you can pass by reading the documentation: `import matplotlib.pyplot as plt; ?plt.hist` In this case, you want to specify `histtype` as `'bar'`. Don't forget to accept and upvote this answer it is solved your issue. – joelostblom Oct 29 '17 at 13:34
  • If I change my code for the histogram to ax = sns.distplot(st,bins=34, kde=False, color = '#007b7f', histtype ='bar',hist_kws=dict(edgecolor="k", linewidth=2)) it gives me an error message saying: _distplot() got an unexpected keyword argument 'histtype'_ . Or do I have to change the histtype within matplotlib? – Jul Oct 29 '17 at 14:25
  • 1
    Only the arguments in the `hist_kws` dictionary are passed "as is" to `plt.hist`. This should work `hist_kws={'histtype':'bar'}`. – joelostblom Oct 29 '17 at 15:10
  • Actually, that is the default, so you should not need to set it. I think the linewidth might be changed. I am looking into this. Maybe open a new question since this is a different topic. Feel free to link it here. – joelostblom Oct 29 '17 at 15:17
  • @Jul It turns out that the default matplotlib style sets the color of the line to the same color as the bin. So although you are actually plotting bars with edgelines, you can't see them. You can override this behavior by specifying the edgecolor to be different from that of your bins, e.g. `hist_kws={'edgecolor':'black'})` – joelostblom Oct 29 '17 at 15:21