Use radar chart in a subplot - matplotlib

I used this code to produce a radar chart: Radar chart with multiple scales on multiple axes; now I want to place this chart in bottom right corner of a 2x1 figures set-up. Using the code below:
fig = pl.figure(figsize=(5, 5))
titles = ['A','B','C','D','E','F']
parameters_list = ['','2','','4','','6','','8','','10']
labels = [parameters_list, parameters_list, parameters_list,parameters_list,parameters_list,parameters_list]
radar = Radar(fig, titles, labels)
pl.subplot(2, 1, 1)
radar.plot([1, 3, 2, 5, 4, 9], "-", lw=2, color="r", alpha=0.4, label="first")
pl.subplot(2, 1, 2)
radar.plot([3, 6, 4, 1, 1, 2], "-", lw=2, color="y", alpha=0.4, label="second")
this yields two blank boxes, while I would like to get two radar charts, one above the other (see link below).
[1]: http://i.stack.imgur.com/oaXzf.png - two blank boxes
If I try to create a single radar chart, the code works correctly (see code and link below):
fig = pl.figure(figsize=(5, 5))
titles = ['A','B','C','D','E','F']
parameters_list = ['','2','','4','','6','','8','','10']
labels = [parameters_list, parameters_list, parameters_list,parameters_list,parameters_list,parameters_list]
radar = Radar(fig, titles, labels)
radar.plot([1, 3, 2, 5, 4, 9], "-", lw=2, color="r", alpha=0.4, label="first")
radar.ax.legend()
[2]: http://i.stack.imgur.com/LnL6e.png - radar chart working correctly
How can I get the two radar charts one above the other? Or how can I insert the radar in a subplot while the other subplots show different kind of chart?

Since you are using the Radar class given in HYRY's answer in Radar chart with multiple scales on multiple axes, here's a solution using that:
fig = pl.figure(figsize=(5, 5))
titles = ['A','B','C','D','E','F']
parameters_list = ['','2','','4','','6','','8','','10']
labels = [parameters_list, parameters_list, parameters_list,parameters_list,parameters_list,parameters_list]
radar = Radar(fig, titles, labels, rect=[0.0, 0.55, 0.5, 0.45])
radar.plot([1, 3, 2, 5, 4, 9], "-", lw=2, color="r", alpha=0.4, label="first")
radar = Radar(fig, titles, labels, rect=[0.0, 0.0, 0.5, 0.45])
radar.plot([3, 6, 4, 1, 1, 2], "-", lw=2, color="y", alpha=0.4, label="second")
Result:
I used the optional rect parameter in that class, which provides sizes for [left, bottom, width, height] relative to the full figure.
Having done so, I don't know why you chose to use a class that is meant for displaying multiple scales, since you only seem to have a single scale (duplicated 6 times in the labels array), so I have to assume you have a good reason.

Related

Same Label displayed for Multiple Data Sets? (MatPlotLib, Python 3.9)

Is it possible to assign data sets to existing data labels with MatPlotLib?
Even if the data sets are assigned the same label name & color, in the actual resulting plot, in the legend they are separate, and have different colors. I'm using Python 3.9.
Let's say we have the following code:
import matplotlib.pyplot as plt
dataX = [[0, 1, 2], [0, 1, 2]]
dataY = [[0, 1, 2], [2, 1, 0]]
dataLabel = "Test Data"
for i in range(len(dataX)):
plt.plot(dataX[i], dataY[i], label = dataLabel)
plt.legend(loc = 'best')
plt.show()
plt.close()
This results in the following plot:
And what I want is for both of these data lines to be the same color, and have them both be labeled as the same "Test Data" label instance.
Is what I'm trying to achieve here even possible with MatPlotLib?
Thanks for reading my post, any help is appreciated!
Define a color:
color = 'blue'
Set this color in plot function. Set label to first line only.
line = plt.plot(dataX[0], dataY[0], label=dataLabel, c=color)
for i in range(1, len(dataX)):
plt.plot(dataX[i], dataY[i], c=color)
plt.legend(loc='best', handles=[line[0]])

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

Get desired wspace and subplots appropriately sized?

I'm trying to make a plot with one panel up top (colspan = 2) and two plots below, with a controlled amount of space between them. I'd like the bounds of the plots to be in alignment. Here's what I'm starting with:
import cartopy
from matplotlib import pyplot
from matplotlib.gridspec import GridSpec
gs = GridSpec(2, 2, height_ratios=[2, 1], hspace=0, wspace=0)
ax0 = pyplot.subplot(gs[0, :], projection=cartopy.crs.LambertConformal())
ax0.add_feature(cartopy.feature.COASTLINE)
ax0.set_extent([-120, -75, 20, 52], cartopy.crs.Geodetic())
ax1 = pyplot.subplot(gs[1, 0], projection=cartopy.crs.LambertConformal())
ax1.add_feature(cartopy.feature.COASTLINE)
ax1.set_extent([-90, -75, 20, 30], cartopy.crs.Geodetic())
ax2 = pyplot.subplot(gs[1, 1], projection=cartopy.crs.LambertConformal())
ax2.add_feature(cartopy.feature.COASTLINE)
ax2.set_extent([-90, -75, 20, 30], cartopy.crs.Geodetic())
pyplot.show()
First problem is that the wspace=0 parameter doesn't take.
Second problem is (at least this is my guess on how to proceed) calculating a height ratio that will make the width of the upper subplot equal the combined width of the lower subplots (plus any wspace).

matplotlib long legend name

I have a following code which produces a graph -
# imports specific to the plots in this example
import sys
import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
resultsDirectory='results'
outputFile=resultsDirectory+".pdf"
axisLabelFontSize=16
borderWidth=0.0
# Twice as wide as it is tall.
fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(111)
# Set up the Grid
[i.set_linewidth(borderWidth) for i in ax.spines.itervalues()]
unsatX=[680,2775,3821,680,4073,941,1202,1463]
unsatY=[1,1,1,4,1,2,2,2]
paretoX=[680, 1203, 1726, 4870]
paretoY=[10,7, 4,1]
satX=[4870,680,1727,1726,1203,680]
satY=[1,13,7,4,7,10]
typeX=[680, 1727]
typeY=[13, 7]
leftX=[680]
leftY=[12]
c = np.rec.fromarrays([paretoX, paretoY], names='x,y')
c.sort()
paretoX=c.x
paretoY=c.y
markrsz=8
l4, = plt.plot(paretoX, paretoY, '#000000', lw=2, label='Pareto Curve(unfolding, period locality)',markersize=markrsz,zorder = 10)
l1, = plt.plot(satX, satY, 'bo', label='Sat Points',markersize=markrsz,zorder = 10)
l2, = plt.plot(unsatX, unsatY, 'ro',marker='s',label='Unsat Points',markersize=markrsz,zorder = 10)
l5, = plt.plot(leftX, leftY, 'gp',label='Proc. count pareto points',markersize=markrsz)
l6, = plt.plot(typeX, typeY, 'w*',label='Modulo pareto points',markersize=markrsz,zorder=10)
leg=plt.legend(bbox_to_anchor=(0.,-0.200, 1., 1.102), loc=3, numpoints=1,
ncol=3, mode="expand", borderaxespad=0., fancybox=True, shadow=True,prop={'size':axisLabelFontSize})
rect = leg.get_frame()
rect.set_facecolor('#cccccc') # a grayscale intensity
#leg.set_frame_on(False)
latency=[680,2775,4870, 680,3821,4868, 680,1727,4341,4864, 680,1203,1726,1203, 680,4073,4334,4595,4856, 941,1202,1463,1724]
processor=[1, 1, 1,13, 1, 1, 7, 7, 1, 1, 4, 4, 4, 7,10,1, 1, 1, 1, 2, 2, 2, 2]
ax.set_xlabel('Period',size=axisLabelFontSize,labelpad=10)
ax.set_ylabel('Processors',size=axisLabelFontSize,labelpad=10)
ax.set_xlim(0, max(latency)+100)
ax.set_ylim(0, max(processor)+1)
# Set Border width zero
[i.set_linewidth(0) for i in ax.spines.itervalues()]
gridLineWidth=0.1
ax.set_axisbelow(False)
gridlines = ax.get_xgridlines()+ax.get_ygridlines()
#ax.set_axisbelow(True)
plt.setp(gridlines, 'zorder', 5)
ax.yaxis.grid(True, linewidth=gridLineWidth, linestyle='-', color='0.6',alpha='0.3')
ax.xaxis.grid(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
fig.savefig(outputFile, format="pdf", bbox_inches='tight')
The legends in the plot get messed up.
Could someone tell me how do i fix a long legend entry which overwrites into area of other entry? What would be ideal if, I could do 3 legend entries in first row and two legend entries in the second row.
Besides the workaround found by the question author, a possibility is to add new lines for long labels:
...
l4, = plt.plot(paretoX, paretoY, '#000000', lw=2,
label='Pareto Curve \n(unfolding, period locality)',markersize=markrsz,zorder = 10)
...
The following modification to the code also solved my problem -
leg=plt.legend(bbox_to_anchor=(0.,-0.350, 1., 1.102), loc=3, numpoints=1, ncol=2 , borderaxespad=0., fancybox=True, shadow=True,prop={'size':axisLabelFontSize})