How to replicate kdeplot's shade in a violinplot? - python

I am trying to replicate seaborn's kdeplot shade=True display on a violin plot.
However, sns.violinplot() does not take the shade argument. I can access the axis collection in the violinplot using ax.collections[0] and then use both set_facecolor() and set_edgecolor() to change the plot colors. However, I'm not sure how to give an alpha value to the facecolor of the plot. I can use set_alpha() but that sets the alpha for both the face and the edge colors.

Related

Making a bar plot using matplotlib.pyplot

I started working with python a couple of weeks ago and thus my knowledge of python is low.
I wish to plot a bar plot with some continuous data, but do not want to fill it with any color. I want to see only the final edges like a line histogram. I can not use white color because I will be overlapping different bar plots with errorbar plots in the same canvas. Any clue on how should I change it to a line histogram and set its line color?
For x-axis, I have used numpy.array.
For y-axis, the height I need is in numpy.histogram form.
I am using following method:
import matplotlib.pyplot as plt
plt.bar(np_array_bins, np_histogram,width=binwidth,
label='data', alpha=0.5, edgecolor = 'red',
color = 'green', linewidth=0.1)
I can't put the real data online but what I have is something like
And I want :
Neglect the data shown. I am concerned only about the style of the plot.
Thanks!
From your description it sounds like you want a step plot or a histogram.
The step plot can be achieved with:
matplotlib.pyplot.step(np_array_bins, np_histogram, color="red", label="data")
The histogram can be achieved with:
matplotlib.pyplot.hist(values, histtype="step", edgecolor="red", label="data")
The important parameter here is histtype, setting it to step draws an unfilled line around the histogram. In this example your original array would be passed in and matplotlib would calculate the bin edges. You can define the bin edges yourself and set other parameters to get more control over the final plot, these are described in the matplotlib docs.

Setting the alpha value of a custom palette in seaborn

I am modifying a piece of code I found in the seaborn documentation, in order to design a palette that is common to matplotlib and seaborn. The code below work great, however, if you plot many points (5000 in my example), the darker color dominates the chart.
A quick fix to that is to set the alpha value of one (or both) colors, to something low.
custom = ["#D1EC9C", "#F1EBF4"]
sns.set_palette(custom)
# construct cmap
my_cmap = ListedColormap(custom)
N = 5000
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()
Does anybody know how to set the alpha value of a custom Seaborn palette?
Thanks
You can set the alpha as part of the colors in your colormap:
custom = [(0xD1/0xFF, 0xEC/0xFF, 0x9C/0xFF, 1), (0xF1/0xFF, 0xEB/0xFF, 0xF4/0xFF, 0.5)]
my_cmap = mpl.colors.ListedColormap(custom)
Here I gave the second color alpha of 0.5; you can do it with the other color if you want.
Note that seaborn is not really involved here. The colors of the plot are determined by the colors you passed in the colormap; seaborn's palette has no impact. The only effect seaborn has on your plot is in the formatting of the background, axes, grid, etc.
As I said in a comment, though, I believe that using alpha for only one color will not make your plot look good. If one color is still opaque, it will still cover up dots of the other color, only more so (because now the other color will be fainter). Also, if you are going to use alpha (and maybe even if you aren't), the grayish color you chose is probably not a good idea, because it is similar to the gray background seaborn provides, making it difficult to distinguish the gray dots from the gray background.

plot dashline using gnuplot

I want to know how to plot the dashlines in this graph. I mean the gray dashlines which are related to the points.

Set the color of the ticker powerlimits using matplotlib

I used the following code to set the power label on the y-axis of my plot to be [1e-3]. (Meaning that all my y-tick values are multiplied by 1e-3).
ax1 = plt.gca()
y_formatter = mpl.ticker.ScalarFormatter(useOffset=True)
y_formatter.set_powerlimits((-3,5))
ax1.yaxis.set_major_formatter(y_formatter)
In addition I want to color all my ticklabels and axis label blue because I am using a twin axis plot.
ax1.set_ylabel('absorpitivity (cm$^{-1}$)',color='b')
for t1 in ax1.get_yticklabels():
t1.set_color('b')
This yields a plot where all my y-tick labels and axis label are blue but the power label at the top of the axis that sets the overall scale is still black. How can I also set the power label at the top of the y-axis to be blue also?
I'd really like to post a picture of my plot, but I don't have enough reputation points to post an image. If this gets up-voted and I get some points I'll edit the post to upload the picture.
You can access it by using
ax1.yaxis.get_offset_text().set_color('b')
For future reference, and if you haven't tried already, you can find these sort of properties by using the ipython interpreter. Using the tab-completion from the ax1 instance you can see a list of methods to access. From here its some guess work but it might help you in future.

color plots different colors on a matrix matplotlib

I have a matrix I have plotted in matplotlib using self.ax.imshow(arr,cmap=plt.cm.Greys_r, interpolation = 'none') As you can see the plots are all the same color.
How can I make the plots, different colours and not just black
The correct link to the color maps is: http://www.loria.fr/~rougier/teaching/matplotlib/#colormaps
You assign the spring color map like this:
self.ax.imshow(arr, cmap = plt.cm.spring, interpolation = 'none').
#tcaswell is of course correct that if your data is binary the color will be binary as well. The color map gives different colors to different z-values. If you want to give the right bottom part a different color from the left bottom part (or whatever), you'll need a different solution. Something with a scatterplot I guess.

Resources