You can specify a color for plots, but Seaborn will mute the color a bit during plotting. Is there a way to turn off this behavior?
Example:
import matplotlib
import seaborn as sns
import numpy as np
# Create some data
np.random.seed(0)
x = np.random.randn(100)
# Set the Seaborn style
sns.set_style('white')
# Specify a color for plotting
current_palette = matplotlib.colors.hex2color('#86b92e')
# Make a plot
g = sns.distplot(x, color=current_palette)
# Show what the color should look like
sns.palplot(current_palette)
I have tried several ways of specifying the color and all available styles in Seaborn, but nothing has worked. I am using iPython notebook and Python 2.7.
It is not using a muted color, its using an alpha/transparency value as part of the default.
Two answers referencing ways to modify matplotlib object transparency:
https://stackoverflow.com/a/4708018
https://stackoverflow.com/a/24549558
seaborn.distplot allows you to pass different parameters for styling (*_kws). Each plot function has it's own parameters and are therefor prefixed by the name of the plot. Eg. histogram has hist_kws. [distplot Reference]
Because the histogram plot is located in matplotlib, we'd have to look at the keyword parameters we can pass. Like you already figured out, you can pass the 'alpha' keyword parameter to get rid of the transparancy of the lines. See reference for more arguments (kwargs section). [pyplot Reference]
Related
I just want the normal default boxplot pattern.
But my boxplot doesn't have color.
What's the reason?
My plot:
But expecting something like this:
use seaborn for plotting boxplots.
import seaborn as sns
def boxplot(x,y):
sns.boxplot(x=x,y=y)
boxplot(x=data_on_x-axis,y=data_on_y-axis)
I created a function so that we can create plots every time it's needed without writing everything again.
just passing the columns will work.
While doing some practice problems using seaborn and a Jupyter notebook, I realized that the distplot() graphs did not have the darker outlines on the individual bins that all of the sample graphs in the documentation have. I tried creating the graphs using Pycharm and noticed the same thing. Thinking it was a seaborn problem, I tried some hist() charts using matplotlib, only to get the same results.
import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset('titanic')
plt.hist(titanic['fare'], bins=30)
yielded the following graph:
Finally I stumbled across the 'edgecolor' parameter on the plt.hist() function, and setting it to black did the trick. Unfortunately I haven't found a similar parameter to use on the seaborn distplot() function, so I am still unable to get a chart that looks like it should.
I looked into changing the rcParams in matplotlib, but I have no experience with that and the following script I ran seemed to do nothing:
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 1
mpl.rcParams['lines.color'] = 'black'
mpl.rcParams['patch.linewidth'] = 1
mpl.rcParams['patch.edgecolor'] = 'black'
mpl.rcParams['axes.linewidth'] = 1
mpl.rcParams['axes.edgecolor'] = 'black'
I was just kind of guessing at the value I was supposed to change, but running my graphs again showed no changes.
I then attempted to go back to the default settings using mpl.rcdefaults()
but once again, no change.
I reinstalled matplotlib using conda but still the graphs look the same. I am running out of ideas on how to change the default edge color for these charts. I am running the latest versions of Python, matplotlib, and seaborn using the Conda build.
As part of the update to matplotlib 2.0 the edges on bar plots are turned off by default. However, you may use the rcParam
plt.rcParams["patch.force_edgecolor"] = True
to turn the edges on globally.
Probably the easiest option is to specifically set the edgecolor when creating a seaborn plot, using the hist_kws argument,
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
For matplotlib plots, you can directly use the edgecolor or ec argument.
plt.bar(x,y, edgecolor="k")
plt.hist(x, edgecolor="k")
Equally, for pandas plots,
df.plot(kind='hist',edgecolor="k")
A complete seaborn example:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.randn(100)
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
plt.show()
As of Mar, 2021 :
sns.histplot(data, edgecolor='k', linewidth=2)
work.
Using hist_kws=dict(edgecolor="k", linewidth=2) gave an error:
AttributeError: 'PolyCollection' object has no property 'hist_kws'
Using the available styles in seaborn could also solve your problem.
Available styles in seaborn are :
ticks
dark
darkgrid
white
whitegrid
I am trying to set the default colormap (not just the color of a specific plot) for matplotlib in my jupyter notebook (Python 3). I found the commands: plt.set_cmap("gray") and mpl.rc('image', cmap='gray'), that should set the default colormap to gray, but both commands are just ignored during execution and I still get the old colormap.
I tried these two codes:
import matplotlib as mpl
mpl.rc('image', cmap='gray')
plt.hist([[1,2,3],[4,5,6]])
import matplotlib.pyplot as plt
plt.set_cmap("gray")
plt.hist([[1,2,3],[4,5,6]])
They should both generate a plot with gray tones. However, the histogram has colors, which correspond to the first two colors of the default colormap. What am I not getting?
Thanks to the comment of Chris, I found the issue, it's not the default colormap that I need to change but the default color cycle. it's described here: How to set the default color cycle for all subplots with matplotlib?
import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler
# Set the default color cycle
colors=plt.cm.gray(np.linspace(0,1,3))
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=colors)
plt.hist([[1,2,3],[4,5,6]])
Since you have two data sets your are passing, you'll need to specify two colors.
plt.hist([[1,2,3],[4,5,6]], color=['black','purple'])
You can make use of the color argument in matplotlib plot function.
import matplotlib.pyplot as plt
plt.hist([[1,2,3],[4,5,6]], color=['gray','gray'])
with this method you have to specify the color scheme for each dataset hence an array of colors as I have put it above.
If you are using a version of matplotlib between prio and 2.0 you need to use rcParams (still working in newer versions):
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'gray'
I'm looking for a way to change the default edge color for matplotlib scatter plots. Typically, things like that would be set through rcParams, and my understanding is that I should set patch.edgecolor. However, that doesn't seem to work. Here is an example:
import numpy as np
from matplotlib import pyplot as plt
x = np.random.randn(50)
y = np.random.randn(50)
with plt.rc_context({'patch.edgecolor': 'white'}):
plt.subplot(121)
plt.scatter(x, y)
plt.subplot(122)
plt.scatter(x, y, edgecolors='white')
In the result, I would like to have both subplots look the same, but instead they look like this:
Note that I don't want to change the code within the with statement, but instead configure matplotlib such that it uses white edges as a fallback if I don't specify anything else. Confusingly, the documentation uses a default argument edgecolors=None in the function signature, but states that the default value for edgecolors is 'face'. How can I change this behaviour?
The rc parameter you're looking for is called
'scatter.edgecolors'
E.g. plt.rcParams['scatter.edgecolors'] = "white"
or
with plt.rc_context({'scatter.edgecolors': 'white'}):.
This is a new feature introduced in #12992 and available from matplotlib 3.1 onwards, which will be released very soon. As of today, you can install the release candidate via pip install --pre --upgrade matplotlib to get this feature.
How do pyplot functions such as show() and savefig() not require a "plot object" to work?
For example, the following code works, but somehow I expect to use a file handler or pass a "plot object" into plt.show() and plot.savefig("venn3.pdf").
from matplotlib import pyplot as plt
from matplotlib_venn import venn3, venn3_circles
# Subset sizes
s = (2,3,4,3,1,0.5,4)
v = venn3(subsets=s, set_labels=('A', 'B', 'C'))
# Subset labels
v.get_label_by_id('100').set_text('Abc')
v.get_label_by_id('010').set_text('aBc')
v.get_label_by_id('110').set_text('ABc')
v.get_label_by_id('001').set_text('Abc')
v.get_label_by_id('101').set_text('aBc')
v.get_label_by_id('011').set_text('ABc')
v.get_label_by_id('111').set_text('ABC')
# Subset colors
v.get_patch_by_id('100').set_color('c')
v.get_patch_by_id('010').set_color('#993333')
v.get_patch_by_id('110').set_color('blue')
# Subset alphas
v.get_patch_by_id('101').set_alpha(0.4)
v.get_patch_by_id('011').set_alpha(1.0)
v.get_patch_by_id('111').set_alpha(0.7)
# Border styles
c = venn3_circles(subsets=s, linestyle='solid')
c[0].set_ls('dotted') # Line style
c[1].set_ls('dashed')
c[2].set_lw(1.0) # Line width
plt.show() # For show() to work without using variable v seems counter-intuitive to me.
plt.savefig("venn3.pdf") # For savefig() to work without using variable v seems counter-intuitive to me.
2[]
matplotlib.pyplot is often called "statemachine". This means that the function it provides do certain things depending on the internal state of pyplot.
In your code, you have created a figure and this is stored as an object; pyplot knows it has one figure.
If you then call other commands, it is assumend that they apply to that one figure which has been created previously, like plt.savefig.
plt.show() would work on all previously created figures (all of them would be shown).
Pyplot uses a global variable to hold the figure object. All pyplot functions work with that variable(s). If you are working interactively, pyplot is perfectly fine since only you will modify that variable. If you are writing multi-threaded or multi-user code pyplot will not work, and you would have to use the layer benath it, which needs the figure object passed in (and is a terrible interface).