Plotting points with different colors using corresponding list of labels - matplotlib

I have the following matrix and vector of labels:
The idea is to plot the points within points according to the labels (1 and -1) in y. assume the calculation of the function true_label works.
M = [5, 10, 15, 25, 70]
for m in M:
points = np.random.multivariate_normal(np.zeros(2), np.eye(2), m)
true_labels = true_label(points)
y = np.where(true_labels, 1, -1)
fig, ax = plt.subplots(1, 1)
colors = ['green', 'red', 'blue']
plt.plot(points, c=y, cmap=matplotlib.colors.ListedColormap(colors))
# red is 1, blue is -1
plt.show()
However I can't seem to get this to work..
AttributeError: Unknown property cmap
is what I keep getting. I've updated matplotlib so I dont really understand why this doesnt work. Any advice on how to get this done easily?

Related

Is the pyplot contour plot using bin centers or bin edges?

I have trouble understanding the documentation for matplotlib.pyplot.contour (https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.contour.html). The documentation says that the arguments for contour are X,Y-coordinates and Z heights.
However, my plot is offset by a half bin width. So it seems to me as if the contour plot needs x_edges and y_edges instead. Could someone help me understand how this works and what is the proper way to use this function?
Documentation says this:
X, Y: array-like, optional
The coordinates of the values in Z.
X and Y must both be 2D with the same shape as Z (e.g. created via numpy.meshgrid), or they must both be 1-D such that len(X) == N is the number of columns in Z and len(Y) == M is the number of rows in Z. [...]
Z(M, N): array-like
The height values over which the contour is drawn.
This is my code:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
values = np.tile(3,(10,2)) # array of [[3,3] ... [3,3]]
X = Y = np.arange(0.5, 6.5, 1) # 0.5 ... 5.5
edges = np.arange(0, 7, 1) # 0, 1, 2, 3, 4, 5, 6
Z, xedges, yedges = np.histogram2d(values[0],
values[1],
bins=(edges, edges))
# len(edges) == 7; len(X) == len(Y) == 6
# Z.shape == (6, 6)
# contour reference says use "The coordinates of the values in Z."
c = ax.contour(X, Y, Z)
plt.savefig('peak_at_3p5.png')
plt.cla()
# however I only get the intended result when using edges
c = ax.contour(xedges[:-1], yedges[:-1], Z)
plt.savefig('peak_at_3p0.png')
Here's the result:
The two figures that the code produces
I also tried using numpy.meshgrid to construct X and Y as 2D arrays but the peak in the figure is still offset by a half bin width.

How to replace colors of a colormap respresenting the two smallest values by white in matplolib?

I am plotting an animated contourf map in matplotlib with a colorbar that changes at each frame. I want to keep the colorbar centered at zero (I am using a diverging colormap) and to do so I use an odd number of levels. The problem is, when I do this, even though the central color of the colormap (cm.seismic) is white, this color does not appear in the colormap. I want to be able to replace the color of the smallest values (the light red and the light blue) by white, so that instead of having one central level whose color is white (zero), I have two (two smallest values).
Instead of providing a colormap, you can provide a list of colors. That list can be calculated from the given colormap, and the middle colors can be set explicitly to white.
Here is an example:
import matplotlib.pyplot as plt
import numpy as np
x = y = np.linspace(-3.0, 3.01, 100)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X ** 2 - Y ** 2)
Z2 = np.exp(-(X - 1) ** 2 - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2
num_levels = 9
colors = plt.cm.seismic(np.linspace(0, 1, num_levels + 1))
colors[num_levels // 2] = [1, 1, 1, 1] # set to white
colors[num_levels // 2 + 1] = [1, 1, 1, 1]
fig, ax1 = plt.subplots(figsize=(10, 5))
CS = ax1.contourf(X, Y, Z, num_levels, colors=colors, origin='lower')
cbar = fig.colorbar(CS, ax=ax1)
plt.show()

mouse-over only on actual data points

Here's a really simple line chart.
%matplotlib notebook
import matplotlib.pyplot as plt
lines = plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.setp(lines,marker='D')
plt.ylabel('foo')
plt.xlabel('bar')
plt.show()
If I move my mouse over the chart, I get the x and y values for wherever the pointer is. Is there any way to only get values only when I'm actually over a data point?
I understood you wanted to modify the behavior of the coordinates displayed in the status bar at the bottom right of the plot, is that right?
If so, you can "hijack" the Axes.format_coord() function to make it display whatever you want. You can see an example of this on matplotlib's example gallery.
In your case, something like this seem to do the trick?
my_x = np.array([1, 2, 3, 4])
my_y = np.array([1, 4, 9, 16])
eps = 0.1
def format_coord(x, y):
close_x = np.isclose(my_x, x, atol=eps)
close_y = np.isclose(my_y, y, atol=eps)
if np.any(close_x) and np.any(close_y):
return 'x=%s y=%s' % (ax.format_xdata(my_x[close_x]), ax.format_ydata(my_y[close_y]))
else:
return ''
fig, ax = plt.subplots()
ax.plot(my_x, my_y, 'D-')
ax.set_ylabel('foo')
ax.set_xlabel('bar')
ax.format_coord = format_coord
plt.show()

matplotlib asymmetric errorbar showing wrong information

I am trying to plot a grouped barplot with asymmetrical errobars. When the error bars a symmetrical, it's producing the correct chart. However, for the asymmetric version, the length of the error bar is wrong.
Here is a minimally reproducible code:
# test with code from documentation
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
# dummy dataframe similar to what I will be using
avg = [20, 35, 30, 35, 27]
men_std_l = [19,33,28,34,25]
men_std_u = [22,37,31,39,29]
df = pd.DataFrame({'avg' :avg, 'low':men_std_l, 'high':men_std_u})
ind = np.arange(df.shape[0]) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, df['avg'], width, yerr=[df['low'].values,df['high'].values], label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('error bar is wrong for asymmetrical, correct otherwise')
ax.legend()
fig.tight_layout()
plt.show()
I have tried the solutions from Asymmetrical errorbar with pandas (getting ValueError: In safezip, len(args[0])=5 but len(args1)=1) and plotting asymmetric errorbars using matplotlib (getting TypeError: Cannot cast array data from dtype('< U1') to dtype('float64') according to the rule 'safe')
Any help is much appreciated.
Answering my own question as I could not understand from the documentation what those lower and upper bounds of errors were. In the hindsight, it should have been clearer if I were not so used to with ggplot in r.
The matplotlib version of asymmetrical errorbar requires the the values to add and subtract from the height of the bars. It does not want the user to provide the upper and lower values, rather the numbers that should be added and subtracted. Therefore, I needed the following:
xel = df['avg'].values - df['low'].values
xeh = df['high'].values - df['avg'].values

Multicolored graph based on data frame values

Im plotting chart based on the data frame as below., I want to show the graph line in different colour based on the column Condition. Im trying the following code but it shows only one colour throughout the graph.
df = pd.DataFrame(dict(
Day=pd.date_range('2018-01-01', periods = 60, freq='D'),
Utilisation = np.random.rand(60) * 100))
df = df.astype(dtype= {"Utilisation":"int64"})
df['Condition'] = np.where(df.Utilisation < 10, 'Winter',
np.where(df.Utilisation < 30, 'Summer', 'Spring'))
condition_map = {'Winter': 'r', 'Summer': 'k', 'Spring': 'b'}
df[['Utilisation','Day']].set_index('Day').plot(figsize=(10,4), rot=90,
color=df.Condition.map(condition_map))
So, I assume you want a graph for each condition.
I would use groupby to separate the data.
# Color setting
season_color = {'Winter': 'r', 'Summer': 'k', 'Spring': 'b'}
# Create figure and axes
f, ax = plt.subplots(figsize = (10, 4))
# Loop over and plot each group of data
for cond, data in df.groupby('Condition'):
ax.plot(data.Day, data.Utilisation, color = season_color[cond], label = cond)
# Fix datelabels
f.autofmt_xdate()
f.legend()
f.show()
If you truly want the date ticks to be rotated 90 degrees, use autofmt_xdate(rotation = 90)
Update:
If you want to plot everything in a single line it's a bit trickier since a line only can have one color associated to it.
You could plot a line between each point and split a line if it crosses a "color boundary", or check out this pyplot example: multicolored line
Another possibility is to plot a lot of scatter points between each point and create a own colormap that represents your color boundaries.
To create a colormap (and norm) I use from_levels_and_colors
import matplotlib.colors
colors = ['#00BEC5', '#a0c483', '#F9746A']
boundaries = [0, 10, 30, 100]
cm, nrm = matplotlib.colors.from_levels_and_colors(boundaries, colors)
To connect each point with next you could shift the dataframe, but here I just zip the original df with a sliced version
from itertools import islice
f, ax = plt.subplots(figsize = (10,4))
for (i,d0), (i,d1) in zip(df.iterrows(), islice(df.iterrows(), 1, None)):
d_range = pd.date_range(d0.Day, d1.Day, freq = 'h')
y_val = np.linspace(d0.Utilisation, d1.Utilisation, d_range.size)
ax.scatter(d_range, y_val, c = y_val, cmap = cm, norm = nrm)
f.autofmt_xdate()
f.show()