I want to use my own colour palette for the hue. I am following the documentation but for some reason, I can't get it right.
Here is my code:
%matplotlib inline
colors = ["#ff4c8b", "#00d8d5",'#f7f7f7']
sns.set_style(rc={'axes.facecolor':'black','figure.facecolor':'black','axes.grid' : False})
s=sns.lineplot(data=data, x="timestamp", y="close.quote",palette=sns.set_palette(sns.color_palette(colors)), hue='contract_ticker_symbol')
Instead of using the three colours I defined, it uses the default hue palette.Can someone tell me what I am doing wrong
Related
How can I set a matplotlib colormap as my color palette in seaborn?
There is a similar question here, but I was unable to implement it:
import matplotlib.colors as mcolors
cmap = mcolors.LinearSegmentedColormap.from_list('jet')
sns.set_palette(cmap)
The second line throws the following error: from_list() missing 1 required positional argument: 'colors'
The easiest way to hack through seaborn's, ahem, anti-jet security system is to use mpl_palette directly:
sns.mpl_palette("jet", 6)
The more general approach for "given a matplotlib colormap, how do I get a list of discrete colors?" is to call the colormap object with vector of intensities between 0 and 1:
plt.cm.jet(np.linspace(0, 1, 6))
Note that you'll get different results there because seaborn clips the two extreme values, doing something like
plt.cm.jet(np.linspace(0, 1, 8))[1:-1]
Also, while I agree that jet is a basically fine source for a small number of discrete colors, unless you really need colors specifically from jet for some reason, I'd suggest looking into the "turbo" colormap, which is constructed basically the same way but with better perceptual properties.
I am using a seaborn regplot with the following code :
sns.relplot(x="NumCustomers", y="TotalSales", hue = 'StoreLabel', data=stores_month, height = 5, aspect = 2, s = 100);
To generate the following plot :
But the colors are hard to differentiate, after scouring google managed to get this code :
sns.color_palette("tab10")
but nothing happens. How can I change the color pallet to something with better contrast? preferably the tab10 palette.
try
sns.set_palette("Blues")
or
sns.set_palette("Dark")
sns.set_style()
1.white
2.dark
3.whitegrid
4.darkgrid
5.ticks
The set palette api must occur before the relPlot
other apis to try catDist, distPlot, catPlot
I think catPlot would work better
I’d like to employ the reverse Spectral colormap ,
https://matplotlib.org/examples/color/colormaps_reference.html
for a lineplot.
This works fine with a hex bin plot::
color_map = plt.cm.Spectral_r
image = plt.hexbin(x,y,cmap=color_map)
but when I do
ax1.plot(x,y, cmp=color_map)
this gives me::
AttributeError: Unknown property cmap
Note, I just want to set the colormap and let matplotliob do the rest; i.e. I don't want to have a color=' argument in the .plot command.
You can have a look at this solution - the third variant is what you want:
https://stackoverflow.com/a/57227821/5972778
You need to know how many lines you're plotting in advance, as otherwise it doesn't know how to choose the colours from the range.
I think that seaborn's color_palette function is very convenient for this purpose. It can be used in a with statement to temporarily set the color cycle for a plot or set of plots.
For example:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
with sns.color_palette("Spectral", n_colors=10):
plt.plot(np.random.rand(5, 10))
You can use with any predefined matplotlib or seaborn colormap, or provide a custom sequence of colors.
I am looking for a way to set a black border on the errorbars in my plot,
The following code:
ax.errorbar(x, y, yerr, fmt='o', label='label',color="#8da0cb",capthick=2, elinewidth=2,zorder=10)
produces:
I find it more aesthetically pleasing if there was a black border around the errorbar like there is on the marker.
Thanks for any help you can provide
Not a great solution, but you could get close by plotting the errorbars again behind your original ones, with a wider line and cap thinkness, and setting the colour of those ones to black. We can make use of the zorder kwarg to put them behind the others.
Heres a MWE:
import matplotlib.pyplot as plt
import numpy as np
# Fake data
x=np.arange(0,5,1)
y=np.ones(x.shape)
yerr = np.ones(x.shape)/4.
# Create figure
fig,ax = plt.subplots(1)
# Set some limits
ax.set_xlim(-1,5)
ax.set_ylim(-2,4)
# Plot errorbars with the line color you want
ax.errorbar(x,y,yerr, fmt='o',color='r',capthick=2,elinewidth=2,capsize=3,zorder=10)
# Plot black errorbars behind (lower zorder) with a wider line and cap thinkness
ax.errorbar(x,y,yerr, fmt='o',color='k',capthick=4,elinewidth=4,capsize=4,zorder=5)
plt.show()
Again, not a perfect solution, but at least it allows you to include it in the legend. This time, rather than plot the errorbars twice, we will use the matplotlib.patheffects module to add a Stroke to the errorbars.
errorbar returns several Line2D and LineCollection objects, so we need to apply the stroke to each of the relevant ones.
import matplotlib.patheffects as path_effects
e = ax.errorbar(x,y,yerr, fmt='o',color='r',capthick=2,elinewidth=2, label='path effects')
e[1][0].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
path_effects.Normal()])
e[1][1].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
path_effects.Normal()])
e[2][0].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
path_effects.Normal()])
ax.legend(loc=0)
As far as I can see from the information provided in the webpage of pyplot I do not see a valid kwargs that exists for what you are asking.
There exists mfc, mec, ms and mew which are markerfacecolor, markeredgecolor, markersize and markeredgewith. It can probably be asked in GitHub so that people take this into consideration and add it in the next version of matplotlib.
Also taking a look at the answer for this question asked in Stackoverflow, I don't believe it can be done.
I'm using Python to plot a couple of graphs and I'm trying to change the formatting and essentially 'brand' the graph. I've managed to change most things using pylab.rcParams[...], but I can't work out how to change the colour of the markers on the axes and the border around the legend. Any help would be much appreciated. The line below is an example of the type of code I've been using to edit other parts. Basically just lines taken from matplotlibrc, but I can't find them to change everything I want.
pylab.rcParams[axes.labelcolor' = '#031F73'
If you just want to use rcParams, the proper parameters are xticks.color and yticks.color. I can't seem to find a key for the legend frame color. You can set that (along with the tick colors) programmatically though.
import pylab
pylab.plot([1,2,3],[4,5,6], label ='test')
lg = pylab.legend()
lg.get_frame().set_edgecolor('blue')
ax = pylab.axes()
for line in ax.yaxis.get_ticklines():
line.set_color('blue')
for line in ax.xaxis.get_ticklines():
line.set_color('blue')
for label in ax.yaxis.get_ticklabels():
label.set_color('blue')
for label in ax.xaxis.get_ticklabels():
label.set_color('blue')
pylab.show()