matplotlib's axvspan converts marker colors in scatter plot but not in ordinary plot - python-3.x

I recently noticed the following phenomen in matplotlib (using v1.3.1 with Python3.3):
Minimal code example
import matplotlib.pyplot as plt
plt.plot(range(5),5*[3],'o', color="r", label="plt.plot")
plt.scatter(range(5),5*[2], color="r", label='plt.scatter')
plt.axvspan(1.5,4.5, color='yellow', alpha=0.5)
ax=plt.gca()
ax.legend(loc=6)
plt.savefig('adjusted_colors.png')
yields a plot with partially adjusted marker colors -- sorry for being not able to show the plot here -- namely markers plotted using pyplot's scatter-function in the area spanned by axvspan.
In contrast, color of markers plotted via pyplot's plot-function is not converted.
Any ideas about how to avoid marker color's adjustment in case plt.scatter is used for plotting, respectively, how to turn it on in case of using plt.plot?

The reason the "colors are adjusted" is not because the color actually changes, but because the scatter result (which is a PathCollection object) is being viewed through a half-transparent colored rectangle that it is underneath. The same doesn't happen to the result of plot (which is a Line2D object) because it lies above the rectangle.
Matplotlib uses an attribute called zorder when it displays artists in a figure (most everything that lives in a matplotlib plot is an artist). The defaults set the plot's zorder to 2, whereas the rectangle and the scatter both have a zorder of 1, and the rectangle defaults to existing "higher" (not sure exactly why, but it is what it is). You can force the scatter points to lie above the rectangle by specifying the zorder keyword:
plt.scatter(range(5),5*[2], color="r", label='plt.scatter', zorder=2)
Or, if you want the opposite, do the same to move the plot points below the rectangle:
plt.plot(range(5),5*[3],'o', color="r", label="plt.plot", zorder=0)

Related

How to use hist_kws in seaborn displot

I want to plot histogram and kde line in the same plot with different color. I want to set green color for the histogram and blue color for the kde line. I managed to figure out using line_kws to change the kde line color but hist_kws is not working on the displot. I have tried using histplot but I am unable to put different color for the hist and the line.
You can use line_kws={'color': ...} to change the color of the kde line. And directly facecolor=... to change the color of the histogram.
The following code has been tested with seaborn 0.11.1 and displot with the default kind (kind='hist') and no hue:
sns.displot(..., facecolor=...) changes the color of the histogram faces
sns.displot(..., edgecolor=...) changes the color of the histogram edges
sns.displot(..., color=...) changes the color of the kde line (when kde=True)
sns.displot(..., line_kws={'lw':...}) changes the parameters of the kdeline, except the color
Here is an example:
import seaborn as sns
penguins = sns.load_dataset('penguins')
sns.displot(data=penguins, x="flipper_length_mm", kde=True, col="species", color='red',
line_kws={'lw': 3}, facecolor='lime', edgecolor='black')
Seaborn's forte is the hue parameter, placing multiple distributions together, for which it is very handy that corresponding kde and histogram get the same color. When using hue, the above coloring gets overridden.
hist_kws is an optional argument in distplot which takes only values in a dictionary. You can use this to set linewidth, edgecolor etc.
Example for your ref

Why the scatter plot is not showing inside pairplot

sns.pairplot(advertising, x_vars=['TV', 'Newspaper', 'Radio'], y_vars='Sales',size=4, aspect=1, kind='scatter')
plt.show()
here when i am calling the pairplot function it is not showing the first plot, if you change the sequence of x_vars then again it wont show the first plot.
But you can see it individually, How can i see all of them in a single pairplot.
This is a bug/feature introduced in seaborn v.0.11.0.
The workaround it to pass diag_kind=None to pairplot

How to draw a coordinate map containing both horizontal and vertical boxplot with matplotlib

I want to draw a picture like this using matplotlib.
How to do that?
Thank you in advance.
Example/i.stack.imgur.com/Vk57A.png
seaborn.jointplot might be what you're looking for. Specify to draw boxplots in the marginal plot and a regplot in the "main plot".
Seaborn builds on top of matplotlib, so you can still modify linecolors, markercolors etc. and draw annotations with plt.annotate.

artifacts in transparent fill when plotting data files with gnuplot [duplicate]

I have noticed that Gnuplot produces ugly artefacts when dealing to filled elements.
One instance is in the palette of the next figure:
Another example is when using filledcurves between two curves defined from points in ASCII files. In this case, you can see that rather than a real solid fill between the lines, the area is filled with number of strips, that only become apparent after zooming quite a bit, but that has very strong impact when rastering the image to png or similar:
This seems to be independent on the terminal. I have tried postscrip, pdfcairo and even tikz. Is there anything that can be done to improve this, or is this a hard limitation of Gnuplot?
Unfortunately, this is an artifact due to antialiasing in the document viewer when you have two filled polygons touching each other. This happens with the filledcurves plotting style, which composes the filled area of many quadrangles, as well as with the pm3d style (as you can see in the colorbox, which shows the same artifacts). See also
problematic Moire pattern in image produced with gnuplot pm3d and pdf output. for a concrete demo case.
There is a workaround, which however is very cumbersome. You must generate a filled polygon object with some script, fill that, use stats to determine the ranges, plot an empty plot (see e.g. Gnuplot - how can I get a figure with no point on it ? (I want to have only the axes, the title and the x- and y- labels)).
I assume, that you have a data file with three columns, and you would plot them with
plot 'test.dat' using 1:2:3 with filledcurves
Using the following very crude python script
from __future__ import print_function
from numpy import loadtxt
import sys
M = loadtxt(sys.argv[1])
print('set object 1 polygon ', end='')
for i in range(0,len(M)):
if (i == 0):
print('from {0},{1} '.format(M[i][0], M[i][1]), end='')
else:
print('to {0},{1} '.format(M[i][0], M[i][1]), end='')
for i in range(len(M)-1,-1,-1):
print('to {0},{1} '.format(M[i][0], M[i][2]), end='')
You can plot the filled curve with
# determine the autoscaling ranges
set terminal push
set terminal unknown
plot 'test.dat' using 1:2, '' using 1:3
set terminal pop
set xrange [GPVAL_X_MIN:GPVAL_X_MAX]
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]
eval(system('python script.py test.dat'))
set object 1 polygon fillstyle solid noborder fillcolor rgb 'red'
plot NaN notitle
That, doesn't yet cover the problem with the jagged colorbox :(

gmtset BASEMAP_FRAME_RGB transparency

I want to not have my basemap axes visible for an image I'm making in GMT with several plots in it. So far I have made them white using:
gmtset BASEMAP_FRAME_RGB = white
However some of the plots partially overlap and the white axes can be seen over the plots - any ideas on how to fix this? By making the basemap frame transparent ideally.
In fact I've realised I can change the order I do the plots in so that the overlap doesn't matter - also replaced -B... with -G255 to plot on a blank square.

Resources