White pcolor introduces white bar - python

I have the following script
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
heatmap = plt.pcolor(data)
plt.show()
Which results into this image
How can I remove the white bar at the very top?

You have to manually set the x and y limits sometimes when you're using pcolor.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
heatmap = plt.pcolor(data)
plt.ylim(0, 24)
plt.show()

I am assuming here that your matrix is not a jagged matrix:
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
nrow, ncol = data.shape
heatmap = plt.pcolor(data)
# put the major ticks
heatmap.axes.set_xticks(np.arange(ncol), minor=False)
heatmap.axes.set_yticks(np.arange(nrow), minor=False)
heatmap.axes.set_xlim(0,ncol) # Assuming a non jagged matrix
heatmap.axes.set_ylim(0,nrow)
plt.show()

Just simple change. np.random.rand(24,7) replace to np.random.rand(25,7)
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(25,7)
heatmap = plt.pcolor(data)
plt.show()
Output:
Or add axis Like plt.axis([0,7,0,24])
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
heatmap = plt.pcolor(data)
plt.axis([0,7,0,24])
plt.show()
Output:

Related

Change boxplot color for boxplot displayed by categories

This is my code and I have tried most functions to change the color and shape of my boxes but nothing seems to work .
Try the palette option:
https://seaborn.pydata.org/generated/seaborn.boxplot.html
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('diamonds')
sns.boxplot(x=df["clarity"], y=df["price"], palette="Reds", showfliers = False)
plt.show()
It's a bit more complicated to use pandas + matplotlib, you have to set the face colors of the boxes:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
df = pd.DataFrame({'Age':np.random.uniform(0,1,20),
'family_history':np.repeat(["No","Yes"],10)})
fig, ax = plt.subplots(1, 1)
bplot = df.boxplot(column="Age",by="family_history",vert=False,
patch_artist=True,return_type='both',ax=ax)
colors = ['lightblue', 'lightgreen']
for patch, color in zip(bplot[0][1]['boxes'], colors):
patch.set_facecolor(color)
Or you can use seaborn:
sns.boxplot(data = df,x = "Age",y="family_history",hue="family_history")

Python matplotlib Y axis labels multiplied by scalar

I am trying to plot an image from numpy.array. Y axe-labels are linear but I need the values multiplied by a number. In this example Y labels should go from 0 to 4000, so multiplied by 2.
Any ideas?
My code so far:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
fig = plt.figure('Nav Panel')
fig.set_size_inches(12, 12)
imgplot = plt.imshow(nupyarray,interpolation='bicubic')
imgplot.set_cmap('Greys_r')
plt.colorbar()
plt.show()
Thanks

Python pyplot x-axis label rotation

I am trying to rotate the xaxis labels but the xticks function below has no effect and the labels overwrite each other
import matplotlib.pyplot as plt
import seaborn as sns
corrmat = X.corr()
plt.xticks(rotation=90)
plt.figure(figsize=(15,16))
ax = sns.heatmap(corrmat, vmin=0, vmax=1)
ax.xaxis.tick_top()
After using suggested code changes: I get the following but I still want to increase the size of the heatmap
setp looks to be the way to go with pyplot (inspiration from this answer). This works for me:
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np; np.random.seed(0)
data = np.random.rand(10, 12)
ax = sns.heatmap(data)
ax.xaxis.tick_top()
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
plt.show()
Obviously I don't have your data, hence the numpy random data, but otherwise the effect is as required:

Seaborn: stripplot x-log scale collapses values

Hi I am trying to use stripplot in seaborn with log scale for the x-axis. It seems that the path I have taken does not work as intended. I would appreciate if someone could help me with that.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(-8, -2, 10)
y = np.linspace(0, 100, 10)
sns.stripplot(x,y)
plt.gca().set_xscale('log')
all the xvalues are collapsed on the right edge of the plot (see plot). I works fine if I set the y-axis to be log.
PS: I would also need to restrict the number of x tick labels.
Thanks.
A scatter plot on a log scale using pyplot.scatter:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(-8, -2, 10)
y = np.linspace(0, 100, 10)
c = np.random.rand(10)
s = 20+np.random.rand(10)*40
plt.scatter(x,y, c=c, s=s, cmap="jet")
plt.gca().set_xscale('log')
plt.xlim(5e-9, 5e-2)
plt.show()
The same scatter plot on a linear scale:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(-8, -2, 10)
y = np.linspace(0, 100, 10)
c = np.random.rand(10)
s = 20+np.random.rand(10)*40
plt.scatter(x,y, c=c, s=s, cmap="jet")
plt.xlim(-0.003, 0.012)
plt.show()

Removing all but one tick on x-axis

I have a graph that I would tick to remove all ticks and their corresponding labels bar the first tick and label on the x-axis. How would I go about this?
import pylab
import numpy as np
import matplotlib.pyplot as plt
a=np.linspace(0,10,10000)
print a
def f(x):
return 1/(1-(x**2)*np.log((1/(x**2)+1)))
b=f(a)
fig, ax = plt.subplots(1)
ax.plot(b,a)
pylab.xlim(0.5, 5)
pylab.ylim(0, 1.5)
fig.show()
you can use ax.set_xticks([1]) to set just one xtick at 1,0.
Also, there's no need to import both pylab and matplotlib.pyplot. The recommended way now is to import matplotlib.pyplot and use all the Axes methods. E.g., you can use ax.set_xlim instead of pylab.xlim.
Here's your full script and output plot:
import numpy as np
import matplotlib.pyplot as plt
a=np.linspace(0,10,10000)
print a
def f(x):
return 1/(1-(x**2)*np.log((1/(x**2)+1)))
b=f(a)
fig, ax = plt.subplots(1)
ax.plot(b,a)
ax.set_xlim(0.5, 5)
ax.set_ylim(0, 1.5)
ax.set_xticks([1])
plt.show()

Categories

Resources