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')
Related
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')
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?
I'm making a contour plot with three arrays: xdata, ydata, and phi. I'd like the face-on axes to correspond to xdata and ydata, and make contours out of phi.
After looking through the matplotlib contour plot example page, I wrote:
X, Y = np.meshgrid(xdata, ydata)
Z1, Z2 = np.meshgrid(phi, phi)
plt.figure(figsize=(10,8))
plt.scatter(xdata, ydata, s=200, c='white', edgecolor='grey', zorder=2)
plt.xlabel("x (degrees)")
plt.ylabel("y (degrees)")
plt.title("Obscuration ellipse $\phi$ (radians)")
CS = plt.contour(X, Y, Z1, zorder=1)
plt.clabel(CS, inline=True, inline_spacing=3, rightside_up=True, fontsize=18)
plt.show()
Here, I'm using zorder to force the scatter points to show up on top of the contour.
On the plot I get, the contours strike through the inline labels:
Some of these inline labels also appear to be stacked on top of others, and in two cases the labels obscure scatter points, despite zorder assignment.
How do I fix my code such that the labels are not strikethrough (as is the case in matplotlib's example page) and zorder is preserved?
I have a file with three columns, lets say, x y z. I need to plot x Vs y but I need to change the color of that (x,y) value depending on its density (stored in z column). I understand that I need to use color map and have to map the values of the color with the z array. I can do that via scatter plot as also shown in this post: How can I make a scatter plot colored by density in matplotlib?
But I do not need the scatter plot, I need the points to be connected, ie I need a line plot. Can it be done in line plot?
It's not possible to connect points from a scatter plot directly. But the same effect can be achieved by plotting a line behind the scatter points.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3,6)
y = np.sin(x)
z = 0.5+np.random.rand(len(x))
fig, ax = plt.subplots()
ax.plot(x, y, color="k", marker=None, zorder=0)
sc = ax.scatter(x, y, c=z, s=100, edgecolor='',zorder=3)
plt.colorbar(sc, label="Density")
plt.show()
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')