Change position of bars of a Pandas histogram - python

I'm plotting several histograms using Pandas hist method.
%matplotlib inline
import pandas as pd
import numpy as np
d = {"columns":["a","b","c","d","e"],"index":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499],"data":[[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0],[0,0,0,-2,0],[0,0,4,0,-2],[3,1,0,-1,-2],[1,2,0,-1,-2],[1,0,0,-1,-2],[1,2,2,0,0],[-1,2,0,-1,1],[-1,0,0,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0],[0,0,0,-2,0],[0,0,4,0,-2],[3,1,0,-1,-2],[1,2,0,-1,-2],[1,0,0,-1,-2],[1,2,2,0,0],[-1,2,0,-1,1],[-1,0,0,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0],[0,0,0,-2,0],[0,0,4,0,-2],[3,1,0,-1,-2],[1,2,0,-1,-2],[1,0,0,-1,-2],[1,2,2,0,0],[-1,2,0,-1,1],[-1,0,0,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0],[0,0,0,-2,0],[0,0,4,0,-2],[3,1,0,-1,-2],[1,2,0,-1,-2],[1,0,0,-1,-2],[1,2,2,0,0],[-1,2,0,-1,1],[-1,0,0,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0],[0,0,0,-2,0],[0,0,4,0,-2],[3,1,0,-1,-2],[1,2,0,-1,-2],[1,0,0,-1,-2],[1,2,2,0,0],[-1,2,0,-1,1],[-1,0,0,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,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,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0],[0,0,0,-2,0],[0,0,4,0,-2],[3,1,0,-1,-2],[1,2,0,-1,-2],[1,0,0,-1,-2],[1,2,2,0,0],[-1,2,0,-1,1],[-1,0,0,-1,0],[-1,2,0,-1,-1],[3,0,1,-1,-1],[-2,0,-1,0,1],[-1,1,0,1,-2],[0,0,0,0,-2],[-1,2,0,-1,0],[0,0,-1,-2,0]]}
df = pd.DataFrame(d['data'], columns=d['columns'], index=d['index'])
bins = np.arange(-5, 5+1, 0.5)
_ = df.hist(figsize=(12, 10), bins=bins)
I'm getting the following histograms:
I'd like to have bars either centered on x-labels (-2, -1, 0, 1, 3) for "a" histogram or have bars at the left of x-labels
I'm looking at http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.hist.html but I can't find a solution.

In order to center the bars you can do
_ = df.hist(figsize=(12, 10), bins=bins, align='left')

Related

matplotlib.pyplot: How to plot single graph with different Colormaps and a Legend?

I am plotting separate figures for each attribute and label for each data sample. Here is the illustration:
As illustrated in the the last subplot (Label), my data contains seven classes (numerically) (0 to 6). I'd like to visualize these classes using a different fancy colors and a legend. Please note that I just want colors for last subplot. How should I do that?
Here is the code of above plot:
x, y = test_data["x"], test_data["y"]
# determine the total number of plots
n, off = x.shape[1] + 1, 0
plt.rcParams["figure.figsize"] = (40, 15)
# plot all the attributes
for i in range(6):
plt.subplot(n, 1, off + 1)
plt.plot(x[:, off])
plt.title('Attribute:' + str(i), y=0, loc='left')
off += 1
# plot Labels
plt.subplot(n, 1, n)
plt.plot(y)
plt.title('Label', y=0, loc='left')
plt.savefig(save_file_name, bbox_inches="tight")
plt.close()
First, just to set up a similar dataset:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random((100,6))
y = np.random.randint(0, 6, (100))
fig, axs = plt.subplots(6, figsize=(40,15))
We could use plt.scatter() to give individual points different marker styles:
for i in range(x.shape[-1]):
axs[i].scatter(range(x.shape[0]), x[:,i], c=y)
Or we could mask the arrays we're plotting:
for i in range(x.shape[-1]):
for j in np.unique(y):
axs[i].plot(np.ma.masked_where(y!=j, x[:,i]), 'o')
Either way we get the same results:
Edit: Ah you've edited your question! You can do exactly the same thing for your last plot only, just modify my code above to take it out of the loop of subplots :)
As suggested, we imitate the matplotlib step function by creating a LineCollection to color the different line segments:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection
from matplotlib.patches import Patch
#random data generation
np.random.seed(12345)
number_of_categories=4
y = np.concatenate([np.repeat(np.random.randint(0, number_of_categories), np.random.randint(1, 30)) for _ in range(20)])
#check the results with less points
#y = y[:10]
x = y[None] * np.linspace(1, 5, 3)[:, None]
x += 2 * np.random.random(x.shape) - 1
#your initial plot
num_plots = x.shape[0] + 1
fig, axes = plt.subplots(num_plots, 1, sharex=True, figsize=(10, 8))
for i, ax in enumerate(axes.flat[:-1]):
ax.plot(x[i,:])
#first we create the matplotlib step function with x-values as their midpoint
axes.flat[-1].step(np.arange(y.size), y, where="mid", color="lightgrey", zorder=-1)
#then we plot colored segments with shifted index simulating the step function
shifted_x = np.arange(y.size+1)-0.5
#and identify the step indexes
idx_steps, = np.nonzero(np.diff(y, prepend=np.inf, append=np.inf))
#create collection of plateau segments
colored_segments = np.zeros((idx_steps.size-1, 2, 2))
colored_segments[:, :, 0] = np.vstack((shifted_x[idx_steps[:-1]], shifted_x[idx_steps[1:]])).T
colored_segments[:, :, 1] = np.repeat(y[idx_steps[:-1]], 2).reshape(-1, 2)
#generate discrete color list
n_levels, idx_levels = np.unique(y[idx_steps[:-1]], return_inverse=True)
colorarr = np.asarray(plt.cm.tab10.colors[:n_levels.size])
#and plot the colored segments
lc_cs = LineCollection(colored_segments, colors=colorarr[idx_levels, :], lw=10)
lines_cs = axes.flat[-1].add_collection(lc_cs)
#scaling and legend generation
axes.flat[-1].set_ylim(n_levels.min()-0.5, n_levels.max()+0.5)
axes.flat[-1].legend([Patch(color=colorarr[i, :]) for i, _ in enumerate(n_levels)],
[f"cat {i}" for i in n_levels],
loc="upper center", bbox_to_anchor=(0.5, -0.15),
ncol=n_levels.size)
plt.show()
Sample output:
Alternatively, you can use broken barh plots or color this axis or even all axes using axvspan.

Matplotlib Colormap Set to Black Below Threshold

I am using imshow to plot a sparse matrix and would like for 0 entries to be colored black. I followed the advice given in this answer, but my plot still has white for 0 entries, which is confusing since the highest weighted entries are hot yellow. Any help is much appreciated.
Here is my code:
cmap1 = cm.get_cmap('inferno', 128)
cmap1.set_under(color='black')
im_plot = ax1.imshow(P_im,cmap=cmap1,norm=LogNorm(vmin=1e-30, vmax=np.max(P_im)+1e-15))
ax1.set_title("Title",size=10)
Check this code:
from matplotlib import cm
import numpy as np
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots(1, 1, figsize = (4, 4))
x = np.random.binomial(n = 1, p = 0.1, size = (20, 20))
cmap1 = cm.get_cmap('Greys_r', 2)
im_plot = ax1.imshow(x, cmap = cmap1)
ax1.set_title("Title", size = 10)
plt.show()
which gives me this image:
I used x = np.random.binomial(n = 1, p = 0.1, size = (20, 20)) to generate a random sparse matrix, replace it with your data.

python distplot with color by values

I wany to create a dist plot (preferably using seaborn) with different colors to different range of values.
I have the vector:
[3,1,2,3,5,6,8,0,0,5,7,0,1, 0.2]
And I want to create a distplot such that all the parts with range 0 to 1 will be red and all the other will be blue.
What is the best way to do so?
I don't know if there is an easy way in seaborn to do this but doing the plot yourself is probably much easier. First you need to get equally sized bins (if you want that) such that the plot looks homogenous (np.histogram). Afterwards it's just a single numpy filter on your observations and the plot.
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
x = np.array([3,1,2,3,5,6,8,0,0,5,7,0,1, 0.2])
# make equal binning through the range, you can adapt the bin size here
counts, bins = np.histogram(x, bins=10)
# here we do the filtering and split the observations based on your color code
x1 = x[(x <= 1) & (x >= 0)]
x2 = x[~((x <= 1) & (x >= 0))]
# finally, do the plot
f, ax = plt.subplots()
ax.hist(x1, bins=bins, color="tab:red")
ax.hist(x2, bins=bins, color="tab:blue")
ax.set(xlabel="Measurement", ylabel="Counts", title="histogram with 2 colors")
sns.despine()
Gives you:
I think you need a scatter plot. In that case, you can try the following solution. Here you first create a column of colors based on your condition and then assign those colors to the scatter plot.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = np.array([3, 1, 2, 3, 5, 6, 8, 0, 0, 5, 7, 0,1, 0.2])
df = pd.DataFrame({'data':data}).reset_index()
df['colors'] = np.where(data<1, 'red', 'blue')
plt.scatter(df['index'], df['data'], c=df['colors'])
Alternative would be to plot directly using DataFrame
data = np.array([3, 1, 2, 3, 5, 6, 8, 0, 0, 5, 7, 0,1, 0.2])
df = pd.DataFrame({'data':data}).reset_index()
colors = np.where(data<1, 'red', 'blue')
df.plot(kind='scatter', x='index', y='data',c=colors)

Matplotlib transData.transform, ValueError: object too deep for desired array when applied to date

I'm trying to update a scatter plot with set_offsets(). My x-axis is defined by a date. I believe I need to use ax.transData.transform() to get the offset values. This seems to work fine for floats but when using a date, I get an error ValueError: object too deep for desired array.
from datetime import date
import matplotlib.pyplot as plt
import numpy as np
x = [date(2010,5,14), date(2013,2,3), date(2014,10,9)]
y = [0.3, 0.5, 0.7]
fig, ax = plt.subplots(1, 1)
ax.set_xlim(date(2010, 1, 1), date(2015, 1, 1))
ax.set_ylim(0, 1)
scatter = plt.scatter(x, y)
offsets = scatter.get_offsets()
new_offsets = np.array(ax.transData.transform([[date(2011,3,4), .6],[date(2012,3,4),.7]]))
new_offsets = np.append(offsets, new_offset, axis=0)
scatter.set_offsets(new_offsets)
I don't think you want to transform anything to pixel space here. That would lead to wrong coordinates once you change the limits or zoom into the plot.
However you will need to convert your dates to numbers before setting them as offsets.
from datetime import date
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
x = [date(2010,5,14), date(2013,2,3), date(2014,10,9)]
y = [0.3, 0.5, 0.7]
fig, ax = plt.subplots(1, 1)
ax.set_xlim(date(2010, 1, 1), date(2015, 1, 1))
ax.set_ylim(0, 1)
scatter = plt.scatter(x, y)
offsets = scatter.get_offsets()
new_offsets = np.array([[mdates.date2num(date(2011,3,4)), .6],[mdates.date2num(date(2012,3,4)),.7]])
new_offsets = np.append(offsets, new_offsets, axis=0)
scatter.set_offsets(new_offsets)
plt.show()

matplotlib hist() autocropping range

I am trying to make a histgram over a specific range but the matplotlib.pyplot.hist() function keeps cropping the range to the bins with entries in them. A toy example:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(-100,100,1000)
nbins = 100
xmin = -500
xmax = 500
fig = plt.figure();
ax = fig.add_subplot(1, 1, 1)
ax.hist(x, bins=nbins,range=[xmin,xmax])
plt.show()
Gives a plot with a range [-100,100]. Why is the range not [-500,500] as specified?
(I am using the Enthought Canopy 1.4 and sorry but I do not have a high enough rep to post an image of the plot.)
Actually, it works if you specify with range an interval shorter than [-100, 100]. For example, this work :
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30, range=(-50, 50))
plt.show()
If you want to plot the histogram on a range larger than [x.min(), x.max()] you can change xlim propertie of the plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30)
plt.xlim(-500, 500)
plt.show()
the following code is for making the same y axis limit on two subplots
f ,ax = plt.subplots(1,2,figsize = (30, 13),gridspec_kw={'width_ratios': [5, 1]})
df.plot(ax = ax[0], linewidth = 2.5)
ylim = [df['min_return'].min()*1.1,df['max_return'].max()*1.1]
ax[0].set_ylim(ylim)
ax[1].hist(data,normed =1, bins = num_bin, color = 'yellow' ,alpha = 1)
ax[1].set_ylim(ylim)

Categories