formatting to group of bars using matplotlib - python

I am trying to learn python mainly for plotting. Here is my sample code:
import numpy as np
import matplotlib.pyplot as plt
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
x=np.arange(len(a[0]))
width=0.2
fig, ax = plt.subplots(figsize=(8,6))
patterns=['/','\\','*']
for bar in a:
ax.bar(x,bar,width,edgecolor='black',color='lightgray', hatch=patterns.pop(0))
x=x+width
plt.show()
Now the problem is that, I need black edge colour for all bars as well as given hatch patter. However, the formatting is applied to first set of bars only. Here is my output. (I am using python3).
What's missing here or what's wrong? I have looked around but did not find any fix.
Update:
I have tried different options :python2, python3 and pdf/png. Here are results
python2 png --fine
python3 png -- shown above
python2 pdf -- see
python3 pdf -- see
I have also tried 'backend' as matplotlib.use('Agg'). I have update my matplotlib version (2.1.0).

There is a current issue in matplotlib 2.1 that only the first bar's edgecolor is applied. The same for the hatch, see this issue. Also see this question.
It may be that you are using matplotlib 2.1 for python3 but not for python2, hence in python2 it works for you. If I run your code in python 2 with matplotlib 2.1 I get the same undesired behaviour.
The issue will be fixed, once matplotlib 2.1.1 is released.
In the meantime, a workaround is to set the edgecolor and hatch on the individual bars:
import numpy as np
import matplotlib.pyplot as plt
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
x=np.arange(len(a[0]))
width=0.2
fig, ax = plt.subplots(figsize=(8,6))
patterns=['/','\\','*']
for y in a:
bars = ax.bar(x,y,width,color='lightgray')
hatch= patterns.pop(0)
for bar in bars:
bar.set_edgecolor("black")
bar.set_hatch(hatch)
x=x+width
plt.show()

It looks something's wrong with edgecolor tuple's alpha value. Set it to 1 will solve the problem.

Related

TwoSlopeNorm in Matplotlib not working as expected

I am trying to create a plot with a diverging colour map which is not symmetric around zero
In this example, the DivergingNorm function is used and produces what I want...
I am using a later version of Matplotlib however (3.5.1) and when I use the suggested code in the link above, I get the following image...
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random((10,10))
data = 10 * (data - 0.8)
fig, ax = plt.subplots()
im = ax.imshow(data, norm=matplotlib.colors.TwoSlopeNorm(0), cmap=plt.cm.seismic, interpolation='none')
fig.colorbar(im)
plt.show()
... which is clearly not right.
Does anyone know how I can reproduce this behaviour from DivergingNorm from older Matplotlib verions? I can't find a solution to this anywhere even though the older behaviour of 'DivergingNorm' is exactly what I want.
I get the same wrong behaviour using this example ---> https://stackoverflow.com/a/69707735/6288682
I should get this...
... but actually get...
Thanks!

Matplotlib styles not being applied

I'm using Spyder v5 within Anaconda and having issues with any styles being applied on charts. Basic chart below (deliberately commented out the sns import to see if it makes any difference -it doesn't)
Basic code below - gives a normal plot as expected
import matplotlib.pyplot as plt
#import seaborn as sns
x_values = [0,1,2,3,4,5,6,7,8,9,10]
x_squared = [x ** 2 for x in x_values]
plt.plot(x_values,x_squared, label = "X-Squared")
plt.plot(x_values,x_cubed, label = "X-Cubed")
plt.show()
If i then try
plt.style.use("seaborn-deep")
plt.plot(x_values,x_squared, label = "X-Squared")
plt.plot(x_values,x_cubed, label = "X-Cubed")
plt.show()
Nothing changes? The style is avaialble (from plt.styles.available) and there is no error when applying the style so something is amiss here.
If i import seaborn then nothing changes
If i try any or all of the following from other solutions in various places in my plt code then nothing much changes (all of the below lines of code work with no errors
%matplotlib inline
plt.rcParams.update(plt.rcParamsDefault)
sns.set()
One time somehow this did seem to force it to change to a seaborn-paper style but that's then the only one i can use and i can't use any other seaborn or other styles at all?? Not sure what forced it to change, think it was sns.set() so I suspect the one it then shows is the default seaborn style, but then why won't it let me use any other styles?
As i'm using SPyder in anaconda the packages are installed to do this plot
I do have Python installed on my base desktop as well... but documentation insists this won't cause a problem and i've not had this problem on any other code ever of having another installation
any tips?

cph.plot(): how to make it bigger?

I need some help to make my cph plot bigger, but unfortunately, it seems like figsize can't be applied on this plot! Can somebody help me please?
I'm using Jupyter Notebook on pandas!
cph.plot()
Here the problem is that the plot function actually plots my features, but they are too much so their names overlap and I can see nothing! I need the plot to be bigger!
Seems like cph.plot() calls matplotlib.pyplot.plot in the back-end. By default, Matplotlib uses the last created figure, so creating a figure with your specified width and height should do the trick:
import matplotlib.pyplot as plt
# 8, 12 => width and height in inches
plt.figure(figsize=(8, 12))
cph.plot(/*your params here*/)
See if this works.
you can try the following command:
import seaborn as sns
sns.set(rc={'figure.figsize':(18,10)})
cph.plot()

matplotlib.pyplot.scatter() loses color when importing seaborn package?

I am working on generating some scatter plot with matplotlib.pyplot.scatter() in jupyter notebook, and I found that if I import seaborn package, the scatter plot will lose its color. I am wondering if anyone has a similar issue?
Here is an example code
import matplotlib.pyplot as plt
import seaborn as sb
plt.scatter(range(4),range(4), c=range(4))
The output is
The scatter plot without seaborn is:
That seems to be the way it behaves. In seaborn 0.3 the default color scale was changed to greyscale. If you change your code to:
plt.scatter(range(4),range(4), c=sb.color_palette())
You will get an image with colors similar to your original.
See the Seaborn docs on choosing color palettes for more info.
Another way to fix this is to specify cmap option for plt.scatter() so that it would not be affected by seaborn:
ax = plt.scatter(range(4),range(4), c=range(4), cmap='gist_rainbow')
plt.colorbar(ax)
The result is:
There are many options for cmap here:
http://matplotlib.org/examples/color/colormaps_reference.html

How do I set the aspect ratio for a plot in Python with Spyder?

I'm brand new to Python, I just switched from Matlab. The distro is Anaconda 2.1.0 and I'm using the Spyder IDE that came with it.
I'm trying to make a scatter plot with equal ratios on the x and y axes, so that this code prints a square figure with the vertices of a regular hexagon plotted inside.
import numpy
import cmath
import matplotlib
coeff = [1,0,0,0,0,0,-1]
x = numpy.roots(coeff)
zeroplot = plot(real(x),imag(x), 'ro')
plt.gca(aspect='equal')
plt.show()
But plt.gca(aspect='equal') returns a blank figure with axes [0,1,0,1], and plt.show() returns nothing.
I think the main problem is that plt.gca(aspect='equal') doesn't just grab the current axis and set its aspect ratio. From the documentation, (help(plt.gca)) it appears to create a new axis if the current one doesn't have the correct aspect ratio, so the immediate fix for this should be to replace plt.gca(aspect='equal') with:
ax = plt.gca()
ax.set_aspect('equal')
I should also mention that I had a little bit of trouble getting your code running because you're using pylab to automatically load numpy and matplotlib functions: I had to change my version to:
import numpy
import cmath
from matplotlib import pyplot as plt
coeff = [1,0,0,0,0,0,-1]
x = numpy.roots(coeff)
zeroplot = plt.plot(numpy.real(x), numpy.imag(x), 'ro')
ax = plt.gca()
ax.set_aspect('equal')
plt.show()
People who are already comfortable with Python don't generally use Pylab, from my experience. In future you might find it hard to get help on things if people don't realise that you're using Pylab or aren't familiar with how it works. I'd recommend disabling it and trying to get used to accessing the functions you need through their respective modules (e.g. using numpy.real instead of just real)

Categories

Resources