How to automatically color matplotlib graphics using same color as pre-existing label? - matplotlib

I am plotting multiple instances of data that use the same label. There are techniques to remove duplicates from the legend, shown here.
However, I additionally want to prevent different colors being used for the same label.
If I call
import matplotlib.pyplot as plt
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a')
plt.plot([1,2,3], [1.5, 4.0, 3.0], label='a')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b')
plt.plot([1,2,3], [1.5, 2.5, 1.0], label='b')
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
unique = [(h, l) for i, (h, l) in enumerate(zip(handles, labels)) if l not in labels[:i]]
ax.legend(*zip(*unique))
I am hoping for a plot like:
However, I get this plot:
Is there a way I can get the former plot without having to explicitly set the colors, like so:
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a', color='C0')
plt.plot([1,2,3], [1.5, 4.0, 3.0], label='a', color='C0')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b', color='C1')
plt.plot([1,2,3], [1.5, 2.5, 1.0], label='b', color='C1')

I think the most straight-forward would be to just remove the labels for all but one of the lines for each label:
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a', color='C0')
plt.plot([1,2,3], [1.5, 4.0, 3.0], color='C0')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b', color='C1')
plt.plot([1,2,3], [1.5, 2.5, 1.0], color='C1')
plt.legend()
You could achieve the same by using an initial underscore for any labels you don't want to show:
plt.plot([1,2,3], [1.0, 3.5, 2.5], label='a', color='C0')
plt.plot([1,2,3], [1.5, 4.0, 3.0], label='_a', color='C0')
plt.plot([1,2,3], [2.0, 3.0, 3.1], label='b', color='C1')
plt.plot([1,2,3], [1.5, 2.5, 1.0], label='_b', color='C1')
plt.legend()
I find this sometimes useful for plotting in a loop (inserting '_' to the start of a label depending on the iteration in the loop, perhaps including a list of colors to set the color for each line - though this would be trivial in your example).
I can't think of a scenario where you actually need matplotlib to "know" that each line is part of the same label. These type of approaches have been fine in my experience.

Related

How to completely remove left and bottom white margins of matplotlib draw?

import numpy as np
from matplotlib import pyplot as plt
data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.figure(figsize=(6, 4))
im = plt.imshow(data, cmap="YlGn")
linewidth = 2
for axis in ['top', 'bottom', 'left', 'right']:
plt.gca().spines[axis].set_linewidth(linewidth)
plt.gca().set_xticks(np.arange(data.shape[1] + 1) - .5, minor=True)
plt.gca().set_yticks(np.arange(data.shape[0] + 1) - .5, minor=True)
plt.gca().grid(which="minor", color="black", linewidth=linewidth)
plt.gca().tick_params(which="minor", bottom=False, left=False)
plt.tight_layout()
plt.gca().set_xticks(ticks=[])
plt.gca().set_yticks(ticks=[])
plt.savefig("test.pdf",
bbox_inches="tight",
transparent="True",
pad_inches=1.0/72.0 * linewidth / 2.0)
This code will output the following pdf, but you can see that there are white borders on the left and bottom, so the pdf is not centered after being inserted into LaTex. How to solve this problem?
plt result:
import numpy as np
from matplotlib import pyplot as plt
data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
plt.figure(figsize=(6, 4))
im = plt.imshow(data, cmap="YlGn")
linewidth = 2
for axis in ['top', 'bottom', 'left', 'right']:
plt.gca().spines[axis].set_linewidth(linewidth)
plt.gca().set_xticks(np.arange(data.shape[1] + 1) - .5, minor=True)
plt.gca().set_yticks(np.arange(data.shape[0] + 1) - .5, minor=True)
plt.gca().grid(which="minor", color="black", linewidth=linewidth)
plt.gca().tick_params(which="minor", bottom=False, left=False)
plt.tight_layout()
plt.gca().set_xticks(ticks=[])
plt.gca().set_yticks(ticks=[])
plt.gca().tick_params(axis="both",
which="major",
left=False,
bottom=False,
labelleft=False,
labelbottom=False)
plt.savefig("test.pdf",
bbox_inches="tight",
transparent="True",
pad_inches=1.0 / 72.0 * linewidth / 2.0)
It was an issue with ticks, solved now.

How to change dtypes of numpy array for tensorflow

I am creating a neural network in tensorflow and I have created the placeholders like this:
input_tensor = tf.placeholder(tf.float32, shape = (None,n_input), name = "input_tensor")
output_tensor = tf.placeholder(tf.float32, shape = (None,n_classes), name = "output_tensor")
During the training process, I was getting the following error:
Traceback (most recent call last):
File "try.py", line 150, in <module>
sess.run(optimizer, feed_dict={X: x_train[i: i + 1], Y: y_train[i: i + 1]})
TypeError: unhashable type: 'numpy.ndarray'
I identified that is because of the different datatypes of my x_train and y_train to the datatypes of the placeholders.
My x_train looks somewhat like this:
array([[array([[ 1., 0., 0.],
[ 0., 1., 0.]])],
[array([[ 0., 1., 0.],
[ 1., 0., 0.]])],
[array([[ 0., 0., 1.],
[ 0., 1., 0.]])]], dtype=object)
It was initially a dataframe like this:
0 [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
1 [[0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]
2 [[0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]
I did x_train = train_x.values to get the numpy array
And y_train looks this:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
x_train has dtype object and y_train has dtype float64.
What I want to know is that how I can change the datatypes of my training data so that it can work well with the tensorflow placeholders. Or please suggest if I am missing something.
It is little hard to guess what shape you want your data to be, but I am guessing one of the two combinations which you might be looking for. I will also try to simulate your data in Pandas dataframe.
df = pd.DataFrame([[[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]],
[[[0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]],
[[[0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]], columns = ['Mydata'])
print(df)
x = df.Mydata.values
print(x.shape)
print(x)
print(x.dtype)
Output:
Mydata
0 [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
1 [[0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]
2 [[0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]
(3,)
[list([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
list([[0.0, 1.0, 0.0], [1.0, 0.0, 0.0]])
list([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0]])]
object
Combination 1
y = [item for sub_list in x for item in sub_list]
y = np.array(y, dtype = np.float32)
print(y.dtype, y.shape)
print(y)
Output:
float32 (6, 3)
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 1. 0.]
[ 1. 0. 0.]
[ 0. 0. 1.]
[ 0. 1. 0.]]
Combination 2
y = [sub_list for sub_list in x]
y = np.array(y, dtype = np.float32)
print(y.dtype, y.shape)
print(y)
Output:
float32 (3, 2, 3)
[[[ 1. 0. 0.]
[ 0. 1. 0.]]
[[ 0. 1. 0.]
[ 1. 0. 0.]]
[[ 0. 0. 1.]
[ 0. 1. 0.]]]
Your x_train is a nested object containing arrays, so you have to unpack it and reshape it. Here's a general purpose hack:
def unpack(a, aggregate=[]):
for x in a:
if type(x) is float:
aggregate.append(x)
else:
unpack(x, aggregate=aggregate)
return np.array(aggregate)
x_train = unpack(x_train.values).reshape(x_train.shape[0],-1)
Once you've got a dense array (y_train is already dense), you can use a function like the following:
def cast(placeholder, array):
dtype = placeholder.dtype.as_numpy_dtype
return array.astype(dtype)
x_train, y_train = cast(X,x_train), cast(Y,y_train)

How to create a new tensor in this situation (derive b from a)?

I have a tensor 'a', I want to modify a element of it.
a = tf.convert_to_tensor([[1.0, 1.0, 1.0],
[1.0, 2.0, 1.0],
[1.0, 1.0, 1.0]], dtype=tf.float32)
And I can got the index of that element.
index = tf.where(a==2)
How to derive 'b' from 'a'?
b = tf.convert_to_tensor([[1.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0]], dtype=tf.float32)
I know that I can't not modify a tensor from this post.
I solve it by using tf.sparse_to_dense()
import tensorflow as tf
a = tf.convert_to_tensor([[1.0, 1.0, 1.0],
[1.0, 2.0, 1.0],
[1.0, 1.0, 1.0]], dtype=tf.float32)
index = tf.where(a > 1)
zero = tf.sparse_to_dense(index, tf.shape(a, out_type=tf.int64), 0., 1.)
update = tf.sparse_to_dense(index, tf.shape(a, out_type=tf.int64), 0., 0.)
b = a * zero + update
with tf.Session() as sess:
print sess.run(b)

Font type in Matplotlib

I am pretty new to Matplotlib, I have been wading through their very nice webpage, and managed to generate something very similar to what I wanted. I am including the code to the plot I want, the only issue I can't fix is to get the xlabel, ylabel and legend also in bold.
Any suggestions would be very welcome,
many thanks
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import numpy as np
from matplotlib.font_manager import FontProperties
from pylab import *
font = FontProperties()
font = {'family' : 'serif',
'weight' : 'bold',
'size' : 12,
}
fig = plt.figure(figsize=(6, 8))
matplotlib.rc('font', **font)
ax = host_subplot(211, axes_class=AA.Axes)
X=[0.061, 0.12, 0.17, 0.23, 0.29, 0.34, 0.4, 0.46, 0.51]
Y=[3.2, 4, 5.6, 7.4, 11.2, 18.6, 28.9, 42.5, 55.9]
Z=[3.2, 4.1, 5.7, 7.6, 11.3, 18.5, 27, 35.6, 46.9]
A=[3.2, 4, 5.6, 7.6, 11.3, 19.2, 30.4, 44.6, 57.7]
B=[3.2, 3.5, 4.8, 6.5, 10.4, 19.7, 32.9, 53.8, 84.2]
C=[3.1, 3.8, 5.6, 8, 13, 26.1, 41.1, 64.3, 103.7]
ax.plot(X, Y, color="red", linewidth=2.5, marker="v", markersize=7)
ax.plot(X, Z, color="orange", linewidth=2.5, marker="p", markersize=7)
ax.plot(X, A, color="yellow", linewidth=2.5, marker="s", markersize=7)
ax.plot(X, B, color="#33CC33", linewidth=2.5, marker="h", markersize=7)
ax.plot(X, C, color="green", linewidth=2.5, marker="D", markersize=7)
ax2 = ax.twin() # ax2 is responsible for "top" axis and "right" axis
ax2.set_xticks([0.061, 0.12, 0.17, 0.23, 0.29, 0.34, 0.4, 0.46, 0.51])
ax2.set_xticklabels(["4", "3","2.4", "2", "1.9", "1.7", "1.6", "1.5", "1.4$\AA$"])
ax2.axis["right"].major_ticklabels.set_visible(False)
ax.set_xlim(0.05, 0.52)
ax.set_ylim(0, 109)
plt.xlabel('1/d$^2$', fontsize=14, fontweight='bold')
plt.ylabel('R$_{meas}$')
plt.legend(("0.05deg/0.05s", "SUM20", "SUM40", "SUM80", "SUM160"))
fontsize=12, fancybox=True, shadow=True)
ax = host_subplot(212, axes_class=AA.Axes)
X=[0.061, 0.12, 0.17, 0.23, 0.29, 0.34, 0.4, 0.46, 0.51]
Y=[65, 62, 46, 35, 23, 13, 8, 4, 2]
Z=[65, 62, 47, 35, 23, 14, 9, 5, 3]
A=[66, 62, 47, 35, 23, 13, 8, 4, 2]
B=[71, 66, 48, 36, 23, 13, 8, 4, 2]
C=[70, 65, 48, 36, 23, 13, 8, 4, 2]
ax.plot(X, Y, color="red", linewidth=2.5, marker="v", markersize=7)
ax.plot(X, Z, color="orange", linewidth=2.5, marker="p", markersize=7)
ax.plot(X, A, color="yellow", linewidth=2.5, marker="s", markersize=7)
ax.plot(X, B, color="#33CC33", linewidth=2.5, marker="h", markersize=7)
ax.plot(X, C, color="green", linewidth=2.5, marker="D", markersize=7)
ax2 = ax.twin() # ax2 is responsible for "top" axis and "right" axis
ax2.set_xticks([0.061, 0.12, 0.17, 0.23, 0.29, 0.34, 0.4, 0.46, 0.51])
ax2.set_xticklabels(["4", "3","2.4", "2", "1.9", "1.7", "1.6", "1.5", "1.4$\AA$"])
ax2.axis["right"].major_ticklabels.set_visible(False)
ax.set_xlim(0.05, 0.52)
fig.subplots_adjust(hspace=0.3)
plt.xlabel('1/d$^2$', fontweight='bold')
plt.ylabel('I/sigma', fontdict=font)
plt.legend(("0.05deg/0.05s", "SUM20", "SUM40", "SUM80", "SUM160"))
plt.show()
Text objects have a weight property: http://matplotlib.org/users/text_props.html
plt.xlabel('This is my label', weight='bold')
plt.title('This is my title', weight='bold')

Matplotlib:empty confusion matrix

Need to plot a confusion matrix with this script. By running it an empty plot appears. Seems I am close to solution. Any hint?
from numpy import *
import matplotlib.pyplot as plt
from pylab import *
conf_arr = [[50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [3.0, 26.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0], [4.0, 1.0, 0.0, 5.0, 0.0, 0.0, 0.0], [3.0, 0.0, 1.0, 0.0, 6.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 47.0, 0.0], [2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.0]]
norm_conf = []
for i in conf_arr:
a = 0
tmp_arr = []
a = sum(i,0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
cb = fig.colorbar(res)
savefig("confmat.png", format="png")
Thanks, I have the plot. Now, the ticks in the x-axes are very small (the graph dimension is: 3 cm x 10 cm or so). How can I enlarge them in order to have a more proportioned graph, lets say 10cm x 10 cm plot? A possible reason is that I visualize the graph as a subplot? Was not able to find the suitable literature to adjust that.
You don't need to clear a current figure (plt.clf()) before adding a new one.
#plt.clf() # <<<<< here
fig = plt.figure()
ax = fig.add_subplot(111)