Change colour of curve according to its y-value in matplotlib [duplicate] - matplotlib

This question already has answers here:
Having line color vary with data index for line graph in matplotlib?
(4 answers)
Set line colors according to colormap
(1 answer)
Closed 8 years ago.
I'm trying to replicate the style of the attached figure using matplotlib's facilities.
Basically, I want to change the colour of the curve according to its y-value using matplotlib.

The plot you've shown doesn't have the color set by the vertical axis of the plot (which is what I would consider the y-value). Instead, it just has 8 different plots overlain, each with a different color, without stating what the color means.
Here's an example of something that looks like your plot:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
# some fake data:
x = np.linspace(0, 2*np.pi, 1000)
fs = np.arange(1, 5.)
ys = np.sin(x*fs[:, None])
for y, f in zip(ys, fs):
plt.plot(x, y, lw=3, c=cm.hot(f/5))
If you actually want the color of one line to change with respect to its value, you have to kind of hack it, because any given Line2D object can only have one color, as far as I know. One way to do this is to make a scatter plot, where each dot can have any color.
x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(2*x)
plt.scatter(x,y, c=cm.hot(np.abs(y)), edgecolor='none')
Notes:
The color vector should range between 0 and 1, so if y.max() > 1, then normalize by it: c=cm.hot(y/y.max()) and make sure it's all positive.
I used edgecolor='none' because by default the scatter markers have a black outline which makes the it look less like a uniform line.
If your data is spaced too far, you'll have to interpolate the data if you don't want gaps between markers.

Related

add text on plt.imshow

I have two ndarrays: Mat, labels
Currently I display Mat:
plt.imshow(Mat, cmap='gray', vmin=0, vmax=1, interpolation='None')
labels has the same shape as Mat, and lables[(i,j)] contains a label of Mat[(i,j)].
How can I show the label on each pixel?
The easiest approach uses Seaborn's heatmap. When annot=True it prints the data values into the cells. But annot= can also be a matrix of labels. In that case it is important to set the print format to string (fmt='s'). annot_kws= can set additional keywords, such as fontsize or color. x and yticklabels can be incorporated in the call to heatmap(), or be set afterwards using matplotlib.
An important benefit of the default coloring is that Sorn uses black on the light colored cells and white on the dark cells.
Here is an example that uses some utf8 characters as labels.
from matplotlib import pyplot as plt
import numpy as np
import seaborn as sns
M, N = 5, 10
mat = np.random.rand(M, N)
labels = np.random.choice(['X', '☀', '★', '♛'], size=(M, N))
ax = sns.heatmap(mat, cmap="inferno", annot=labels, annot_kws={'fontsize': 16}, fmt='s')
plt.show()
PS: There is a matplotlib example in the documentation to create something similar without Seaborn. It can be easily adapted to print strings from a different matrix, and also a test can be added to change the color depending on the cell darkness.

Matplotlib: How to plot an empty circle in an scatter plot using pandas plot api? [duplicate]

This question already has answers here:
How to do a scatter plot with empty circles in Python?
(6 answers)
Closed 4 years ago.
I'm trying to plot a scatter plot with pandas api where each point is an empty circle, just with border color and transparency. I've tried a lot of tweaks in this code:
ax = ddf.plot.scatter(
x='espvida',
y='e_anosestudo',
c=ddf['cor'],
alpha=.2,
marker='o');
The generated plot looks like this:
If you look closely at the points:
you'll see that they have a transparent fill color and a border. I'd like it to have just a transparent border. Hou would I do it?
I can't seem to get it to work with DataFrame.plot.scatter; it doesn't seem to respect the facecolors='none' kwarg, likely because some default color argument is being passed to plt.scatter.
Instead, fall back to matplotlib, specifying facecolors='none' and setting the edgecolors to the column in your df that represents the color.
Sample Data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'x': np.random.normal(1,1,1000),
'y': np.random.normal(1,1,1000),
'color': list('rgby')*250})
plt.scatter(df.x.values, df.y.values, facecolors='none', edgecolors=df['color'], alpha=0.2, s=100)
plt.show()
From the matplotlib scatter doc:
edgecolors : color or sequence of color, optional, default: 'face'. The edge color of the marker. Possible values:
'face': The edge color will always be the same as the face color.
'none': No patch boundary will be drawn.
A matplotib color.
For non-filled markers, the edgecolors kwarg is ignored and forced to 'face' internally
Try add: edgecolors='none':
ax = ddf.plot.scatter(
x='espvida',
y='e_anosestudo',
c=ddf['cor'],
alpha=.2,
marker='o',
edgecolors='none);

How do I use colourmaps with variable alpha in a Seaborn kdeplot without seeing the contour lines?

Python version: 3.6.4 (Anaconda on Windows)
Seaborn: 0.8.1
Matplotlib: 2.1.2
I'm trying to create a 2D Kernel Density plot using Seaborn but I want each step in the colourmap to have a different alpha value. I had a look at this question to create a matplotlib colourmap with alpha values: Add alpha to an existing matplotlib colormap.
I have a problem in that the lines between contours are visible. The result I get is here:
I thought that I had found the answer when I found this question: Hide contour linestroke on pyplot.contourf to get only fills. I tried the method outlined in the answer (using set_edgecolor("face") but it did not work in this case. That question also seemed to be related to vector graphics formats and I am just writing out a PNG.
Here is my script:
import numpy as np
import seaborn as sns
import matplotlib.colors as cols
import matplotlib.pyplot as plt
def alpha_cmap(cmap):
my_cmap = cmap(np.arange(cmap.N))
# Set a square root alpha.
x = np.linspace(0, 1, cmap.N)
my_cmap[:,-1] = x ** (0.5)
my_cmap = cols.ListedColormap(my_cmap)
return my_cmap
xs = np.random.uniform(size=100)
ys = np.random.uniform(size=100)
kplot = sns.kdeplot(data=xs, data2=ys,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=30)
plt.savefig("example_plot.png")
Guided by some comments on this question I have tried some other methods that have been successful when this problem has come up. Based on this question (Matplotlib Contourf Plots Unwanted Outlines when Alpha < 1) I have tried altering the plot call to:
sns.kdeplot(data=xs, data2=ys,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=30,
antialiased=True)
With antialiased=True the lines between contours are replaced by a narrow white line:
I have also tried an approach similar to this question - Pyplot pcolormesh confused when alpha not 1. This approach is based on looping over the PathCollections in kplot.collections and tuning the parameters of the edges so that they become invisible. I have tried adding this code and tweaking the linewidth -
for thing in kplot.collections:
thing.set_edgecolor("face")
thing.set_linewidth(0.01)
fig.canvas.draw()
This results in a mix of white and dark lines - .
I believe that I will not be able to tune the line width to make the lines disappear because of the variable width of the contour bands.
Using both methods (antialiasing + linewidth) makes this version, which looks cool but isn't quite what I want:
I also found this question - Changing Transparency of/Remove Contour Lines in Matplotlib
This one suggests overplotting a second plot with a different number of contour levels on the same axis, like:
kplot = sns.kdeplot(data=xs, data2=ys,
ax=ax,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=30,
antialiased=True)
kplot = sns.kdeplot(data=xs, data2=ys,
ax=ax,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=35,
antialiased=True)
This results in:
This is better, and almost works. The problem here is I need variable (and non-linear) alpha throughout the colourmap. The variable banding and lines seem to be a result of the combinations of alpha when contours are plotted over each other. I also still see some clear/white lines in the result.

how to attach a color gradient to every bar using matplotlib [duplicate]

This question already has an answer here:
how to plot gradient fill on the 3d bars in matplotlib
(1 answer)
Closed 5 years ago.
The standard way in which I create bar plots in matplotlib is using ax.bar3d. This has the disadvantage of returning block of solid color. Does anyone know how to attach a gradient color to every bar? I am think in reproducing Fig. 1 from https://arxiv.org/pdf/1706.09289.pdf.
Try this:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(18,12))
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid(np.arange(5),np.arange(3))
z_data = np.random.rand(3,5)
colors = ['r','g','b'] # colors for every line of y
# plot colored 3d bars
for i in xrange(3): # cycle though y
# I multiply one color by len of x (it is 5) to set one color for y line
ax.bar3d(x_data[i], y_data[i], z_data[i], 1, 1, z_data[i], alpha=0.1, color=colors[i]*5)
# or use random colors
# ax.bar3d(x_data[i], y_data[i], z_data[i], 1, 1, z_data[i], alpha=0.1, color=[np.random.rand(3,1),]*5)
plt.show()
Result:

secondary Y axis position matplotlib

I need to change the secondary Y axis position on a matplotlib plot.
It's like a subplot inside the same plot.
In the image below, my secondary Y axis starts at the same position as first y axis. I need that the secondary Y axis starts about at the "18" position of the first Y axis, with a smaller scale (red line).
If I understand the question, you want a twinx axis, as #kikocorreoso says, but you also want to compress it, so it only takes up the upper portion of the y axis.
You can do this by just setting the ylim larger than you need it, and explicitly setting the yticks. Here's an example with some random data
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(np.random.randint(0,5),4,25) for _ in range(25)] # some random data
fig=plt.figure()
ax1=fig.add_subplot(111)
ax2=ax1.twinx()
ax1.set_ylim(-5,25)
ax2.set_ylim(0,14)
ax2.set_yticks([10,12,14]) # ticks below 10 don't show up
ax1.boxplot(data)
ax2.plot(np.linspace(0,26,50),12.+2.*np.sin(np.linspace(0,2.*np.pi,50))) # just a random line
plt.show()
If I understood correctly seeing the figure you posted you want a second y-axis. You can do this using plt.twinx. An example could be like the following:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.twinx()
plt.plot([5,4,5])
plt.show()