I am making a function in python which allows me to create two parallel graphs and they share their 2 axes:
def PlotManager(data1,data2,fig):
f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)
#Plot1 sopra
x_axis = data1.index
#Plot and shade the area between the upperband and the lower band grey
ax1.fill_between(x_axis,data1['Upper'],data1['Lower'], color = 'grey', alpha= 0.5)
#Plot the closing price and the moving average
ax1.plot(x_axis,data1['Close'],color = 'gold',lw = 3,label = 'Close Price', alpha= 0.5)
ax1.plot(x_axis,data1['SMA'],color = 'blue',lw = 3,label = 'Simple Moving Average', alpha= 0.5)
ax1.scatter(x_axis,data1['Buy'],color="green", lw=3,label="Buy",marker = "^", alpha=1)
ax1.scatter(x_axis,data1['Sell'],color="red", lw=3,label="Sell",marker = "v", alpha = 1)
#Set the title and show the image
ax1.set_title("Bollinger Band for Amazon")
plt.xticks(rotation = 45)
#Plot 2 Sotto
ax2.set_title('RSI_Plot')
ax2.plot(x_axis,data2['RSI'])
ax2.axhline(0,linestyle='--',alpha=0.5, color="grey")
ax2.axhline(10,linestyle='--',alpha=0.5, color="orange")
ax2.axhline(20,linestyle='--',alpha=0.5, color="green")
ax2.axhline(30,linestyle='--',alpha=0.5, color="red")
ax2.axhline(70,linestyle='--',alpha=0.5, color="red")
ax2.axhline(80,linestyle='--',alpha=0.5, color="green")
ax2.axhline(90,linestyle='--',alpha=0.5, color="orange")
ax2.axhline(100,linestyle='--',alpha=0.5, color="grey")
But gives me the cannot unpack non-iterable AxesSubplot object error:
[Command: python -u C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py]
C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\BollingerBandsFinal.py:63: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
ax = f.add_subplot(111)
C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\BollingerBandsFinal.py:63: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
ax = f.add_subplot(111)
Traceback (most recent call last):
File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\cbook\__init__.py", line 196, in process
func(*args, **kwargs)
File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 951, in _start
self._init_draw()
File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 1743, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "C:\Users\Nicolò\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 1766, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py", line 48, in animate
PlotManager(BollingerBands(df,f),RSI(df,f2),f)
File "C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\mostraGrafici.py", line 7, in PlotManager
f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)
TypeError: cannot unpack non-iterable AxesSubplot object
How can i handle to this error?
The value of plt.subplots(2, 1, ...) is a tuple figure, array(subplot0, subplot1) so that you can unpack correctly to a figure and two subplots.
On the contrary the value of fig.subplots(2, 1, ...) is subplot0, subplot1 (because you ALREADY have the figure…) and when you try to unpack it's equivalent to
f = subplot0
ax0, ax1 = subplot1
and this leads to TypeError: cannot unpack non-iterable AxesSubplot object
Because you are not using the object labeled as f in the following, you should write
ax1, ax2 = fig.subplots(2, 1, sharey=True,sharex=True)
Related
Using the python seaborn package I was trying to plot the nested bar graphs with three different y-axes as shown in the below figure:
And the code that I have used is :
import matplotlib.pyplot as plt
from matplotlib import rc
import numpy as np
import seaborn as sns
#plt.style.use(['science'])
rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
rc('text', usetex=True)
HV = [388, 438]
YS = [1070, 1200]
UTS = [1150, 1400]
Z = [15, 12.5]
x = [1, 2]
fig, ax1 = plt.subplots(figsize=(5, 5.5))
colors=sns.color_palette("rocket",4)
ax1 = sns.barplot(x[0],YS[0],color="blue")
ax1 = sns.barplot(x[0],color="blue")
ax1 = sns.barplot(x[1],YS[1],color="blue")
ax1 = sns.barplot(x[1],UTS[1],color="blue")
ax2 = ax1.twinx()
ax2 = sns.barplot(x[0], HV[0],color="green")
ax2 = sns.barplot(x[1], HV[1],color="green")
ax3 = ax1.twinx()
ax3 = sns.barplot(x[0],Z[0],color="red")
ax3 = sns.barplot(x[1],Z[1],color="red")
#ax3.spines['right'].set_position(('outward',60))
ax3.spines['right'].set_position(('axes',1.15))
ax1.set_ylabel("First",color="blue")
ax2.set_ylabel("Second",color="green")
ax3.set_ylabel("Third",color="red")
ax1.tick_params(axis='y',colors="blue")
ax2.tick_params(axis='y',colors="green")
ax3.tick_params(axis='y',colors="red")
ax2.spines['right'].set_color("green")
ax3.spines['right'].set_color("red")
ax3.spines['left'].set_color("blue")
plt.show()
And I'm getting the following error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/seaborn/utils.py", line 531, in categorical_order
order = values.cat.categories
AttributeError: 'int' object has no attribute 'cat'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/seaborn/utils.py", line 534, in categorical_order
order = values.unique()
AttributeError: 'int' object has no attribute 'unique'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/sspenkulinti/these/thesis_E185_fatigue/test_matrix/E185_properties_AB_HT.py", line 21, in <module>
ax1 = sns.barplot(x[0],YS[0],color="blue")
File "/usr/lib/python3/dist-packages/seaborn/categorical.py", line 3147, in barplot
plotter = _BarPlotter(x, y, hue, data, order, hue_order,
File "/usr/lib/python3/dist-packages/seaborn/categorical.py", line 1614, in __init__
self.establish_variables(x, y, hue, data, orient,
File "/usr/lib/python3/dist-packages/seaborn/categorical.py", line 200, in establish_variables
group_names = categorical_order(groups, order)
File "/usr/lib/python3/dist-packages/seaborn/utils.py", line 536, in categorical_order
order = pd.unique(values)
File "/usr/lib/python3/dist-packages/pandas/core/algorithms.py", line 395, in unique
values = _ensure_arraylike(values)
File "/usr/lib/python3/dist-packages/pandas/core/algorithms.py", line 204, in _ensure_arraylike
inferred = lib.infer_dtype(values, skipna=False)
File "pandas/_libs/lib.pyx", line 1251, in pandas._libs.lib.infer_dtype
TypeError: 'int' object is not iterable
The error is because you can't call sns.barplot with a single number as first parameter. The x-values need to be a list.
To get want you want using seaborn, the data needs to be presented as if it comes from a dataframe. hue_order is needed to preserve enough space for each of the bars, even when nothing is plotted there.
import matplotlib.pyplot as plt
from matplotlib import rc
import numpy as np
import seaborn as sns
HV = [388, 438]
YS = [1070, 1200]
UTS = [1150, 1400]
Z = [15, 12.5]
x = ["As built", "200ºC-850ºC"]
names = ['YS', 'UTS', 'HV', 'Z']
fig, ax1 = plt.subplots(figsize=(9, 5.5))
colors = sns.color_palette("tab10", len(names))
sns.barplot(x=x + x, y=YS + UTS, hue=[names[0]] * len(x) + [names[1]] * len(x),
hue_order=names, palette=colors, alpha=0.7, ax=ax1)
# ax1 will already contain the full legend, the third handle needs to
# be updated to show the hatching
ax1.legend_.legendHandles[2].set_hatch('///')
ax2 = ax1.twinx()
sns.barplot(x=x, y=HV, hue=[names[2]] * len(x), hue_order=names, palette=colors, hatch='//', alpha=0.7, ax=ax2)
ax2.legend_.remove() # seaborn automatically creates a new legend
ax3 = ax1.twinx()
sns.barplot(x=x, y=Z, hue=[names[3]] * len(x), hue_order=names, palette=colors, alpha=0.7, ax=ax3)
ax3.legend_.remove()
ax3.spines['right'].set_position(('axes', 1.15))
ax1.set_ylabel("First", color=colors[0])
ax2.set_ylabel("Second", color=colors[2])
ax3.set_ylabel("Third", color=colors[3])
ax1.tick_params(axis='y', colors=colors[0])
ax2.tick_params(axis='y', colors=colors[2])
ax3.tick_params(axis='y', colors=colors[3])
ax2.spines['right'].set_color(colors[2])
ax3.spines['right'].set_color(colors[3])
plt.tight_layout()
plt.show()
I'm trying to animate a particle collision animation in one axes object while simultaneously plotting some data in another axis object (using matplotlib.gridspec).
I set up a test environment with test data, simplifying the collision to just appearing circles in axes 1 (matplotlib.patches.Circle objects) and plotting a sine function in axes 2 (matplotlib.lines.Line2D object).
The animation works for one axes at a time, but I can't run both axes animations simultaneously.
I think the problem is related to the return objects in the init() and animate(i) function. The "matplotlib.patches" and "matplotlib.lines" object have problems in combination with the animation, but I can't figure out why.
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.animation import FuncAnimation
from matplotlib.gridspec import GridSpec
#%% setup test data
xy = (0.5, 0.5)
r = 0.02
i = np.arange(10,110)
#%% setup figure
fig = plt.figure(figsize = (8,8))
gs = GridSpec(6,4, figure=fig, wspace=0, hspace=0)
ax0 = fig.add_subplot(gs[0:4,0:4])
ax1 = fig.add_subplot(gs[5:6,0:4])
ax1.set_xlim(0,100)
# assigning labels
circles = []
ln1, = ax1.plot([],[], 'r')
def init():
ln1.set_data([],[])
circles = []
return circles, ln1
def animate(i):
xy = (np.random.random(), np.random.random())
circles.append(ax0.add_patch(Circle(xy,r)))
x = np.linspace(0,100,100)
y = np.sin(x)
mask1 = x < i
x1 = x[mask1]
y1 = y[mask1]
ln1.set_data(x1, y1)
return circles, ln1
ani = FuncAnimation(fig, animate, init_func=init, frames = i, blit=True, interval = 1)
plt.show()
That is the error message I receive:
Traceback (most recent call last):
File "C:\Users\flofr\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process
func(*args, **kwargs)
File "C:\Users\flofr\anaconda3\lib\site-packages\matplotlib\animation.py", line 953, in _start
self._init_draw()
File "C:\Users\flofr\anaconda3\lib\site-packages\matplotlib\animation.py", line 1741, in _init_draw
a.set_animated(self._blit)
AttributeError: 'list' object has no attribute 'set_animated'
Traceback (most recent call last):
File "C:\Users\flofr\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process
func(*args, **kwargs)
File "C:\Users\flofr\anaconda3\lib\site-packages\matplotlib\animation.py", line 1269, in _handle_resize
self._init_draw()
File "C:\Users\flofr\anaconda3\lib\site-packages\matplotlib\animation.py", line 1741, in _init_draw
a.set_animated(self._blit)
AttributeError: 'list' object has no attribute 'set_animated'
I tried to test if the return values are correct for init() and it seems to be the case.
Input:
def init():
ln1.set_data([],[])
circles = []
circles.append(ax0.add_patch(Circle(xy,r)))
return ln1, circles
a, b = init()
print(a)
print(b)
Output:
Line2D(_line0)
[<matplotlib.patches.Circle object at 0x000001E7DE16F548>]
Here are the three states of the simulation. Imgage one shows the layout, image two the axes 1 simulation (circles) and image three the axes 2 simulation (sine).
I have the following data and labels I am transforming through PCA.
The labels are only 0 or 1.
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import seaborn as sns
import numpy as np
fields = ["Occupancy", "Temperature", "Humidity", "Light", "CO2", "HumidityRatio", "NSM", "WeekStatus"]
df = pd.read_csv('datatraining-updated.csv', skipinitialspace=True, usecols=fields, sep=',')
#Get the output from pandas as a numpy matrix
final_data=df.values
#Data
X = final_data[:,1:8]
#Labels
y = final_data[:,0]
#Normalize features
X_scaled = StandardScaler().fit_transform(X)
#Apply PCA on them
pca = PCA(n_components=7).fit(X_scaled)
#Transform them with PCA
X_reduced = pca.transform(X_scaled)
Then, I just want to show, in a 3D graph, the 3 PCA features with highest variance, I can find them as follows
#Show variable importance
importance = pca.explained_variance_ratio_
print('Explained variation per principal component:
{}'.format(importance))
After that, I want to plot only the top-3 highest variance features. So, I previously select them in the code below
X_reduced=X_reduced[:, [0, 4, 5]]
Ok, here is my problem: I can plot them without the legend. When I try to plot them using the following code
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax = fig.gca(projection='3d')
colors = ("red", "gray")
for data, color, group in zip(X_reduced, colors, y):
dim1,dim2,dim3=data
ax.scatter(dim1, dim2, dim3, c=color, edgecolors='none',
label=group)
plt.title('Matplot 3d scatter plot')
plt.legend(y)
plt.show()
I get the following error:
plot_data-3d-pca.py:56: UserWarning: Requested projection is different from current axis projection, creating new axis with requested projection.
ax = fig.gca(projection='3d')
plot_data-3d-pca.py:56: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
ax = fig.gca(projection='3d')
Traceback (most recent call last):
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py", line 307, in idle_draw
self.draw()
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3agg.py", line 76, in draw
self._render_figure(allocation.width, allocation.height)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3agg.py", line 20, in _render_figure
backend_agg.FigureCanvasAgg.draw(self)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1709, in draw
renderer, self, artists, self.suppressComposite)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "/home/unica-server/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 292, in draw
reverse=True)):
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 291, in <lambda>
key=lambda col: col.do_3d_projection(renderer),
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 545, in do_3d_projection
ecs = (_zalpha(self._edgecolor3d, vzs) if self._depthshade else
File "/home/unica-server/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 847, in _zalpha
rgba = np.broadcast_to(mcolors.to_rgba_array(colors), (len(zs), 4))
File "<__array_function__ internals>", line 6, in broadcast_to
File "/home/unica-server/.local/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 182, in broadcast_to
return _broadcast_to(array, shape, subok=subok, readonly=True)
File "/home/unica-server/.local/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 127, in _broadcast_to
op_flags=['readonly'], itershape=shape, order='C')
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (0,4) and requested shape (1,4)
My y's shape is (8143,) and my X_reduced's shape is (8143,3)
What is my mistake?
EDIT: The data I am using can be found here
The first warning Requested projection is different from current axis projection
is because you are trying to change the projection of an axis after its creation with ax = fig.gca(projection='3d') but you cannot. Set the projection at creation instead.
To fix the second error, replace edgecolors='none' by edgecolors=None.
The following corrected code works for me.
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d') # set projection at creation of axis
# ax = fig.gca(projection='3d') # you cannot change the projection after creation
colors = ("red", "gray")
for data, color, group in zip(X_reduced, colors, y):
dim1,dim2,dim3=data
# replace 'none' by None
ax.scatter(dim1, dim2, dim3, c=color, edgecolors=None, label=group)
plt.title('Matplot 3d scatter plot')
plt.legend(y)
plt.show()
EDIT : Above is my answer to what I understood of the original question. Below is a looped version of mad's own answer.
class_values = [0, 1]
labels = ['Empty', 'Full']
n_class = len(class_values)
# allocate lists
index_class = [None] * n_class
X_reduced_class = [None] * n_class
for i, class_i in enumerate(class_values) :
# get where are the 0s and 1s labels
index_class[i] = np.where(np.isin(y, class_i))
# get reduced PCA for each label
X_reduced_class[i] = X_reduced[index_class[i]]
colors = ['blue', 'red']
# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
ids_plot = [0, 4, 5]
for i in range(n_class) :
# get the three interesting columns
data = X_reduced_class[i][:, ids_plot]
ax.scatter(data[:,0], data[:,1], data[:,2], c=colors[i], edgecolor='k', s=40, label=labels[i])
ax.set_title("Data Visualization with 3 highest variance dimensions with PCA")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])
ax.legend()
plt.show()
I solved the error in a different way.
I did not know that, for each label, I had to do a different scatterplot. Thanks to this post I found the answer.
My solution was first to separate the labels and data from one class, and then do the same for the other class. Finally, I plot them separately with different scatterplots. So, firstly I identify the different labels (I have only two labels, 0 or 1) and their data (their corresponding Xs).
#Get where are the 0s and 1s labels
index_class1 = np.where(np.isin(y, 0))
index_class2 = np.where(np.isin(y, 1))
#Get reduced PCA for each label
X_reducedclass1=X_reduced[index_class1][:]
X_reducedclass2=X_reduced[index_class2][:]
Then, I will plot each PCA reduced vectors from each class in different scatterplots
colors = ['blue', 'red']
# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
scatter1=ax.scatter(X_reducedclass1[:, 0], X_reducedclass1[:, 4], X_reducedclass1[:, 5], c=colors[0], cmap=plt.cm.Set1, edgecolor='k', s=40)
scatter2=ax.scatter(X_reducedclass2[:, 0], X_reducedclass2[:, 4], X_reducedclass2[:, 5], c=colors[1], cmap=plt.cm.Set1, edgecolor='k', s=40)
ax.set_title("Data Visualization with 3 highest variance dimensions with PCA")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])
#ax.legend(np.unique(y))
ax.legend([scatter1, scatter2], ['Empty', 'Full'], loc="upper right")
plt.show()
Which gives me this beautiful image
Of course, such a code can be simplified with a for loop too (altough I have no idea how to do that).
After defining ax1=fig1.add_subplot(111) and plotting 8 data series with associated label values, I used the following line of code to add a legend.
ax1.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
I have used this method many times before without a problem, but on this occasion it produces an error saying IndexError: tuple index out of range
Traceback (most recent call last):
File "interface_tension_adhesion_plotter.py", line 45, in <module>
ax1.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 564, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend.py", line 386, in __init__
self._init_legend_box(handles, labels, markerfirst)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend.py", line 655, in _init_legend_box
fontsize, handlebox))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend_handler.py", line 119, in legend_artist
fontsize, handlebox.get_transform())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend_handler.py", line 476, in create_artists
self.update_prop(coll, barlinecols[0], legend)
IndexError: tuple index out of range
I have no idea why this is happening and would really appreciate suggestions.
1. If data is intact and arrays are not empty, this code works perfectly.
fig = plt.gcf()
ax=fig.add_subplot(111)
for i in range(8):
x = np.arange(10)
y = i + random.rand(10)
yerr = .1*y
l = .1*i
ax.errorbar(x,y,yerr=yerr,label="adhsion={:02.1f}".format(l))
ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
2. I had same error when I applied a filter to my data and got empty arrays. This could be reproduced as follows:
fig = plt.gcf()
ax=fig.add_subplot(111)
for i in range(8):
x = np.arange(10)
y = i + random.rand(10)
yerr = .1*y
l = .1*i
if i == 7:
ind = np.isnan(y)
y = y[ind]
x = x[ind]
yerr = yerr[ind]
ax.errorbar(x,y,yerr=yerr,label="adhsion={:02.1f}".format(l))
ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
This code gives identical Traceback as in the question. Empty array for errors results in wrong handles for errorbars.
Workaround mentioned by #crevell:
handles, labels = ax.get_legend_handles_labels()
handles = [h[0] for h in handles]
ax.legend(handles, labels,loc='center left', bbox_to_anchor=(1.0, 0.5))
It works, but legend appears without errorbar lines.
So one should check the data supplied to the matplotlib errorbar function.
So I using this code to create a donut chart with python (inspired in this Donut plot recipe):
def make_pie(sizes, text,colors,labels):
import matplotlib.pyplot as plt
import numpy as np
col = [[i/255. for i in c] for c in colors]
fig, ax = plt.subplots()
ax.axis('equal')
width = 0.35
kwargs = dict(colors=col, startangle=180)
outside, _ = ax.pie(sizes, radius=1, pctdistance=1-width/2,labels=labels,**kwargs)
plt.setp( outside, width=width, edgecolor='white')
kwargs = dict(size=20, fontweight='bold', va='center')
ax.text(0, 0, text, ha='center', **kwargs)
plt.show()
c1 = (226,33,7)
c2 = (60,121,189)
make_pie([257,90], "Gender (AR)",[c1,c2],['M','F'])
which results in:
My problem is that now I want the respective percentages. For that I was simply adding the argument:
autopct='%1.1f%%'
like this:
kwargs = dict(colors=col, startangle=180,autopct='%1.1f%%')
but this results in the following error:
Traceback (most recent call last):
File "draw.py", line 30, in <module>
make_pie([257,90], "Gender (AR)",[c1,c2],['M','F'])
File "draw.py", line 13, in make_pie
outside, _ = ax.pie(sizes, radius=1, pctdistance=1-width/2,labels=labels,**kwargs)
ValueError: too many values to unpack
So, what am I doing wrong?
From the docstring:
If *autopct* is not *None*, return the tuple (*patches*,
*texts*, *autotexts*), where *patches* and *texts* are as
above, and *autotexts* is a list of
:class:`~matplotlib.text.Text` instances for the numeric
labels.
So if you want to unpack the result of pie() using autopct you need 3 values:
kwargs = dict(colors=col, startangle=180, autopct='%1.1f%%')
outside, _, _ = ax.pie(sizes, radius=1, pctdistance=1-width/2,
labels=labels,**kwargs)
Or maybe it will be more robust without unpacking so it works with or without autopct:
outside = ax.pie(sizes, radius=1, pctdistance=1-width/2,
labels=labels,**kwargs)[0]