Path Effects for Markers in Matplotlib's Errorbar - python

I'm trying to include an outline to lines plotted with plt.errorbar(). As suggested by Can I give a border (outline) to a line in matplotlib plot function?\,, I tried to use path_effects, however I need a different path_effect for the markers and the line.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patheffects as pe
x = np.arange(20)
y = x**1.3
# Two lines.
fig, ax = plt.subplots()
ax.errorbar(x, y, fmt='-o', lw=5, mew=3, ms=5, c='k')
ax.errorbar(x, y, fmt='-o', lw=2, mew=0, ms=5, c='r')
# Single line with path_effects.
y += 10
ax.errorbar(x, y, fmt='-o', lw=2, mew=0, ms=5, c='b',
path_effects=[pe.Stroke(linewidth=5, foreground='k'), pe.Normal()])
which produces the following output:
.
The difference between these methods is that in the former, the outline appears as a constant width around both the line and the marker, while in the one using path_effects, the outline is thicker around the markers. Is there a way to adjust the outline linewidth for the marker and the line separately?

Related

matplotlib break line near a marker

Is there are posibility to break a line in a matplotlib diagram. I made a sketch for better understanding what I mean.
If by 'intercept' you mean the gaps or buffers between the points and the lines, I'd add an edge around the points like so:
import matplotlib.pyplot as plt
x = y = [1, 2, 3]
fig, ax = plt.subplots()
ax.plot(x, y, lw=5, c='k') # The black line.
ax.scatter(x, y, s=200, c='lime', ec='white', lw=5, zorder=10)
ax.set_xlim(0.5, 3.5)
ax.set_ylim(0.5, 3.5)
This prodices:
I'm plotting the line and points separately, controlling the layer order with zorder. There are always lots of ways to do things, but I find markers easier to control with plt.scatter().

Give a individual zorder value to every marker in a matplotlib scatter plot

I have a matplotlib scatter plot with many markers:
plt.scatter(x_position,y_position,c=z_position,s=90, cmap=cm.bwr,linewidth=1,edgecolor='k')
Sometimes the markers overlap. I want the zorder of each to be based on the z_position of the individual marker.
Is this possible in a scatterplot or would I have to have an separate line for each data point with its own zorder value?
Thank you.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0,1,0,1])
y = np.array([0,0,1,1])
z = np.array([8,4,6,2])
If you now call
plt.scatter(x, y, c=z, s=1000, marker="X",
cmap=plt.cm.bwr, linewidth=1, edgecolor='k')
markers overlap:
The last marker in the arrays is drawn last, hence the one with z=2 is in front.
You can sort the arrays by z to change the order of appearance.
order = np.argsort(z)
plt.scatter(x[order], y[order], c=z[order], s=1000, marker="X",
cmap=plt.cm.bwr, linewidth=1, edgecolor='k')

Color axis spine with multiple colors using matplotlib

Is it possible to color axis spine with multiple colors using matplotlib in python?
Desired output style:
You can use a LineCollection to create a multicolored line. You can then use the xaxis-transform to keep it fixed to the xaxis, independent of the y-limits. Setting the actual spine invisible and turning clip_on off makes the LineCollection look like the axis spine.
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
fig, ax = plt.subplots()
colors=["b","r","lightgreen","gold"]
x=[0,.25,.5,.75,1]
y=[0,0,0,0,0]
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments,colors=colors, linewidth=2,
transform=ax.get_xaxis_transform(), clip_on=False )
ax.add_collection(lc)
ax.spines["bottom"].set_visible(False)
ax.set_xticks(x)
plt.show()
Here is a slightly different solution. If you don't want to recolor the complete axis, you can use zorder to make sure the colored line segments are visible on top of the original axis.
After drawing the main plot:
save the x and y limits
draw a horizontal line at ylims[0] between the chosen x-values with the desired color
clipping should be switched off to allow the line to be visible outside the strict plot area
zorder should be high enough to put the new line in front of the axes
the saved x and y limits need to be put back, because drawing extra lines moved them (alternatively, you might have turned off autoscaling the axes limits by calling plt.autoscale(False) before drawing the colored axes)
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
for i in range(10):
plt.plot(x, np.sin(x*(1-i/50)), c=plt.cm.plasma(i/12))
xlims = plt.xlim()
ylims = plt.ylim()
plt.hlines(ylims[0], 0, 10, color='limegreen', lw=1, zorder=4, clip_on=False)
plt.hlines(ylims[0], 10, 20, color='crimson', lw=1, zorder=4, clip_on=False)
plt.vlines(xlims[0], -1, 0, color='limegreen', lw=1, zorder=4, clip_on=False)
plt.vlines(xlims[0], 0, 1, color='crimson', lw=1, zorder=4, clip_on=False)
plt.xlim(xlims)
plt.ylim(ylims)
plt.show()
To highlight an area on the x-axis, also axvline or axvspan can be interesting. An example:
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 25, 100)
for i in range(10):
plt.plot(x, np.sin(x)*(1-i/20), c=plt.cm.plasma(i/12))
plt.axvspan(10, 20, color='paleturquoise', alpha=0.5)
plt.show()

No color when I make python scatter color plot using third variable to define color

I try to make colorful scatter plot using third variable to define color. It is simple to use the following code:
plt.scatter(mH, mA, s=1, c=mHc)
plt.colorbar()
plt.show()
But I do not have many choices to modify the frame of the plot. I am trying the following code to make colorful scatter plot, at the same time I try to optimize the frame of the plot:
import numpy as np
import math
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator
fig, ax = plt.subplots()
cax = ax.scatter(mH,mA,s=0.5,c=mHc) ### mH, mA, mHC are the dataset
fig.colorbar(cax)
minor_locator1 = AutoMinorLocator(6)
minor_locator2 = AutoMinorLocator(6)
ax.xaxis.set_minor_locator(minor_locator1)
ax.yaxis.set_minor_locator(minor_locator2)
ax.tick_params('both', length=10, width=2, which='major')
ax.tick_params('both', length=5, width=2, which='minor')
ax.set_xlabel(r'$m_H$')
ax.set_ylabel(r'$m_A$')
ax.set_xticks([300,600,900,1200,1500])
ax.set_yticks([300,600,900,1200,1500])
plt.savefig('mH_mA.png',bbox_inches='tight')
plt.show()
But the plot I got is black-white. It looks like the problem lies in the marker size argument, but I do not have much idea how to correct it. I want to have smaller marker size. Anyone can offer me some idea to approach this issue. Thanks.
size=0.5 is extremely small - probably all you are seeing is the marker outlines. I would suggest you increase the size a bit, and perhaps pass edgecolors="none" to turn off the marker edge stroke:
import numpy as np
from matplotlib import pyplot as plt
n = 10000
x, y = np.random.randn(2, n)
z = -(x**2 + y**2)**0.5
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y, s=5, c=z, cmap="jet", edgecolors="none")
You might also want to experiment with making the points semi-transparent using the alpha= parameter:
ax.scatter(x, y, s=20, c=z, alpha=0.1, cmap="jet", edgecolors="none")
It can be difficult to get scatter plots to look nice when you have such a massive number of overlapping points. I would be tempted to plot your data as a 2D histogram or contour plot instead, or perhaps even a combination of a scatter plot and a contour plot:
density, xe, ye = np.histogram2d(x, y, bins=20, normed=True)
ax.hold(True)
ax.scatter(x, y, s=5, c=z, cmap="jet", edgecolors="none")
ax.contour(0.5*(xe[:-1] + xe[1:]), 0.5*(ye[:-1] + ye[1:]), density,
colors='k')

How to plot individual points without curve in python?

I want to plot individual data points with error bars on a plot, but I don't want to have the curve. How can I do this? Are there some 'invisible' line style or can I set the line style colourless (but the marker still has to be visible)?
So this is the graph I have right now:
plt.errorbar(x5,y5,yerr=error5, fmt='o')
plt.errorbar(x3,y3,yerr=error3, fmt='o')
plt.plot(x3_true,y3_true, 'r--', label=(r'$\lambda = 0.3$'))
plot(x5_true, y5_true, 'b--', label=(r'$\lambda = 0.5$'))
plt.plot(x5,y5, linestyle=':', marker='o', color='red') #this is the 'ideal' curve that I want to add
plt.plot(x3,y3, linestyle=':', marker='o', color='red')
I want to keep the two dashed curve but I don't want the two dotted curve. How can I do this? And how can I change the color of the markers so I can have red points for the red curve, blue points for the blue curve?
You can use scatter:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 10)
y = np.sin(x)
plt.scatter(x, y)
plt.show()
Alternatively:
plt.plot(x, y, 's')
EDIT: If you want error bars you can do:
plt.errorbar(x, y, yerr=err, fmt='o')

Categories