Supblots to include radar plot - python

I'm running into issues with some subplots. I've provided some sample code to generate the types of plots I would like to create. I'd like these to be the same size, side by side.
I'm am having a really hard time figuring out how to create the subplots though. I keep running into some issues with the thetagrids here. This is what i've tried. I can get these to work seprarately, but cant figure out how to combine them. Eventually I might want a third plot as well.
import numpy as np
import matplotlib.pyplot as plt
## Plot 1
x1 = np.array([0, 1, 2, 3])
y1 = np.array([7, 2, 4, 2])
plt.subplot(1, 2, 1)
plt.figure(figsize=(5, 5))
plt.scatter(x1, y1)
# plt.show()
### Plot 2
# make up data for plot
polar_list = ['a', 'b', 'c', 'd', 'a']
polar_points = [4, 3, 6, 7, 4]
# modify lists for plots
label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(polar_list))
plt.figure(figsize=(5, 5))
plt.subplot(1, 2, 2, polar=True)
plt.plot(label_loc, polar_points, label='DataLable')
plt.title('DataLable comparison', size=20, y=1.05)
lines, labels = plt.thetagrids(np.degrees(label_loc), labels=polar_list)
plt.legend()
plt.show()

You are creating a new figure every time you call plt.figure(). Just place one at the very beginning and then the plt.subplot() will add subplots to the figures.
import numpy as np
import matplotlib.pyplot as plt
## Plot 1
x1 = np.array([0, 1, 2, 3])
y1 = np.array([7, 2, 4, 2])
plt.figure(figsize= (5, 5))
plt.subplot(1, 2, 1)
plt.scatter(x1, y1)
# plt.show()
### Plot 2
# make up data for plot
polar_list = ['a', 'b', 'c', 'd', 'a']
polar_points = [4, 3, 6, 7, 4]
# modify lists for plots
label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(polar_list))
plt.subplot(1, 2, 2, polar=True)
plt.plot(label_loc, polar_points, label='DataLable')
plt.title('DataLable comparison', size=20, y=1.05)
lines, labels = plt.thetagrids(np.degrees(label_loc), labels=polar_list)
plt.legend()
plt.show()

Related

how to label sections in a bar chart [duplicate]

This question already has answers here:
Stacked Bar Chart with Centered Labels
(2 answers)
Horizontal stacked bar plot and add labels to each section
(3 answers)
How to annotate each segment of a stacked bar chart
(1 answer)
Closed 1 year ago.
What is the most simple way to label all the sections?
x = ['A', 'B', 'C', 'D']
y1 = np.array([2, 4, 5, 1])
y2 = np.array([1, 0, 2, 3])
y3 = np.array([4, 1, 1, 1])
plt.bar(x, y1, color='#d67ed0')
plt.bar(x, y2, color='#e6ad12', bottom=y1)
plt.bar(x, y3, color='#13c5ed', bottom=y1+y2)
plt.show()
Like "A"-Violet labeled as "2" on the plot
The easiest way to label each colored section is by using a legend. Assign a category to each color in the bar using the label argument in the plt.bar function. Then use the plt.legend() function at the end of the code to display the legend.
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
x = ['A', 'B', 'C', 'D']
y1 = np.array([2, 4, 5, 1])
y2 = np.array([1, 0, 2, 3])
y3 = np.array([4, 1, 1, 1])
# increase figure size
plt.figure(figsize = (10,7))
# add labels to each color
plt.bar(x, y1, color='#d67ed0', label = 'Cars')
plt.bar(x, y2, color='#e6ad12', bottom=y1, label = 'Buses')
plt.bar(x, y3, color='#13c5ed', bottom=y1+y2, label = 'Trains')
plt.legend(loc = 1, fontsize = 18)
plt.show()

Determine plot size with grid Matplotlib

I'm plotting some data using plt.scatter(), and I want to change the size of the plot which is it on, however the only results which come up when you search 'change plot size' are things to do with changing the figure's size, which I am not looking to do.
To visualise my issue, I have a reproducible example where I'm trying to plot 4 points on a 10x10 grid, however the size of the scatter plot is determined by the data not the grid
The two graphs above demonstrate my problem, I am trying to plot the four points on the left graph on the 10x10 grid seen on the right graph. I have added in a datapoint at (10, 10) to show this.
My code is currently:
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
fig = plt.figure()
ax = fig.gca()
ax.set_xticks(np.arange(0, 11, 1))
ax.set_yticks(np.arange(0, 11, 1))
plt.grid()
plt.scatter(x, y)
Which produces the left graph.
IIUC:
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.grid()
plt.scatter(x, y)
Output:
Just change the limits of x and y axes:
plt.xlim(0,11)
plt.ylim(0,11)

Plot multiple histograms as a grid

I am trying to plot multiple histograms on the same window using a list of tuples. I have managed to get it to sketch only 1 tuple at a time and I just can't seem to get it to work with all of them.
import numpy as np
import matplotlib.pyplot as plt
a = [(1, 2, 0, 0, 0, 3, 3, 1, 2, 2), (0, 2, 3, 3, 0, 1, 1, 1, 2, 2), (1, 2, 0, 3, 0, 1, 2, 1, 2, 2),(2, 0, 0, 3, 3, 1, 2, 1, 2, 2),(3,1,2,3,0,0,1,2,3,1)] #my list of tuples
q1,q2,q3,q4,q5,q6,q7,q8,q9,q10 = zip(*a) #split into [(1,0,1,2,3) ,(2,2,2,0,1),..etc] where q1=(1,0,1,2,3)
labels, counts = np.unique(q1,return_counts=True) #labels = 0,1,2,3 and counts the occurence of 0,1,2,3
ticks = range(len(counts))
plt.bar(ticks,counts, align='center')
plt.xticks(ticks, labels)
plt.show()
As you can see from the above code, I can plot one tuple at a time say q1,q2 etc but how do I generalise it so that it plots all of them.
I've tried to mimic this python plot multiple histograms, which is exactly what I want however I had no luck.
Thank you for your time :)
You need to define a grid of axes with plt.subplots taking into account the amount of tuples in the list, and how many you want per row. Then iterate over the returned axes, and plot the histograms in the corresponding axis. You could use Axes.hist, but I've always preferred to use ax.bar, from the result of np.unique, which also can return the counts of unique values:
from matplotlib import pyplot as plt
import numpy as np
l = list(zip(*a))
n_cols = 2
fig, axes = plt.subplots(nrows=int(np.ceil(len(l)/n_cols)),
ncols=n_cols,
figsize=(15,15))
for i, (t, ax) in enumerate(zip(l, axes.flatten())):
labels, counts = np.unique(t, return_counts=True)
ax.bar(labels, counts, align='center', color='blue', alpha=.3)
ax.title.set_text(f'Tuple {i}')
plt.tight_layout()
plt.show()
You can customise the above to whatever amount of rows/cols you prefer, for 3 rows for instance:
l = list(zip(*a))
n_cols = 3
fig, axes = plt.subplots(nrows=int(np.ceil(len(l)/n_cols)),
ncols=n_cols,
figsize=(15,15))
for i, (t, ax) in enumerate(zip(l, axes.flatten())):
labels, counts = np.unique(t, return_counts=True)
ax.bar(labels, counts, align='center', color='blue', alpha=.3)
ax.title.set_text(f'Tuple {i}')
plt.tight_layout()
plt.show()

Suggestions to plot overlapping lines in matplotlib?

Does anybody have a suggestion on what's the best way to present overlapping lines on a plot? I have a lot of them, and I had the idea of having full lines of different colors where they don't overlap, and having dashed lines where they do overlap so that all colors are visible and overlapping colors are seen.
But still, how do I that.
I have the same issue on a plot with a high degree of discretization.
Here the starting situation:
import matplotlib.pyplot as plt
grid=[x for x in range(10)]
graphs=[
[1,1,1,4,4,4,3,5,6,0],
[1,1,1,5,5,5,3,5,6,0],
[1,1,1,0,0,3,3,2,4,0],
[1,2,4,4,3,2,3,2,4,0],
[1,2,3,3,4,4,3,2,6,0],
[1,1,3,3,0,3,3,5,4,3],
]
for gg,graph in enumerate(graphs):
plt.plot(grid,graph,label='g'+str(gg))
plt.legend(loc=3,bbox_to_anchor=(1,0))
plt.show()
No one can say where the green and blue lines run exactly
and my "solution"
import matplotlib.pyplot as plt
grid=[x for x in range(10)]
graphs=[
[1,1,1,4,4,4,3,5,6,0],
[1,1,1,5,5,5,3,5,6,0],
[1,1,1,0,0,3,3,2,4,0],
[1,2,4,4,3,2,3,2,4,0],
[1,2,3,3,4,4,3,2,6,0],
[1,1,3,3,0,3,3,5,4,3],
]
for gg,graph in enumerate(graphs):
lw=10-8*gg/len(graphs)
ls=['-','--','-.',':'][gg%4]
plt.plot(grid,graph,label='g'+str(gg), linestyle=ls, linewidth=lw)
plt.legend(loc=3,bbox_to_anchor=(1,0))
plt.show()
I am grateful for suggestions on improvement!
Just decrease the opacity of the lines so that they are see-through. You can achieve that using the alpha variable. Example:
plt.plot(x, y, alpha=0.7)
Where alpha ranging from 0-1, with 0 being invisible.
imagine your panda data frame is called respone_times, then you can use alpha to set different opacity for your graphs. Check the picture before and after using alpha.
plt.figure(figsize=(15, 7))
plt.plot(respone_times,alpha=0.5)
plt.title('a sample title')
plt.grid(True)
plt.show()
Depending on your data and use case, it might be OK to add a bit of random jitter to artificially separate the lines.
from numpy.random import default_rng
import pandas as pd
rng = default_rng()
def jitter_df(df: pd.DataFrame, std_ratio: float) -> pd.DataFrame:
"""
Add jitter to a DataFrame.
Adds normal distributed jitter with mean 0 to each of the
DataFrame's columns. The jitter's std is the column's std times
`std_ratio`.
Returns the jittered DataFrame.
"""
std = df.std().values * std_ratio
jitter = pd.DataFrame(
std * rng.standard_normal(df.shape),
index=df.index,
columns=df.columns,
)
return df + jitter
Here's a plot of the original data from Markus Dutschke's example:
And here's the jittered version, with std_ratio set to 0.1:
Replacing solid lines by dots or dashes works too
g = sns.FacetGrid(data, col='config', row='outputs', sharex=False)
g.map_dataframe(sns.lineplot, x='lag',y='correlation',hue='card', linestyle='dotted')
Instead of random jitter, the lines can be offset just a little bit, creating a layered appearance:
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
grid = list(range(10))
graphs = [[1, 1, 1, 4, 4, 4, 3, 5, 6, 0],
[1, 1, 1, 5, 5, 5, 3, 5, 6, 0],
[1, 1, 1, 0, 0, 3, 3, 2, 4, 0],
[1, 2, 4, 4, 3, 2, 3, 2, 4, 0],
[1, 2, 3, 3, 4, 4, 3, 2, 6, 0],
[1, 1, 3, 3, 0, 3, 3, 5, 4, 3]]
fig, ax = plt.subplots()
lw = 1
for gg, graph in enumerate(graphs):
trans_offset = offset_copy(ax.transData, fig=fig, x=lw * gg, y=lw * gg, units='dots')
ax.plot(grid, graph, lw=lw, transform=trans_offset, label='g' + str(gg))
ax.legend(loc='upper left', bbox_to_anchor=(1.01, 1.01))
# manually set the axes limits, because the transform doesn't set them automatically
ax.set_xlim(grid[0] - .5, grid[-1] + .5)
ax.set_ylim(min([min(g) for g in graphs]) - .5, max([max(g) for g in graphs]) + .5)
plt.tight_layout()
plt.show()

Matplotlib: Getting different colors in data lines with error bars

I am trying to draw two data lines with error bars, each having the same color as the data line. However, I get another thin line with a color I have not specified in each data line when I add an error bar.
Also, I would like to make the caps of the error bars thicker but the option capthick is not valid here.
Could anybody please help me fix these issues?
This is my code.
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
xaxis = [1, 2, 3]
mean1 = [1,2,3.6]
se1 = [0.2, 0.5, 0.9]
mean2 = [10, 29, 14]
se2 = [3, 4, 2]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('X', fontsize = 16)
ax.set_ylabel('Y', fontsize = 16)
ax.axis([0, 5, 0, 35])
ax.plot(xaxis, mean1, 'r--', linewidth = 4)
ax.errorbar(xaxis, mean1, yerr = se1, ecolor = 'r', elinewidth = 2, capsize = 5)
ax.plot(xaxis, mean2, 'b--', linewidth = 4)
ax.errorbar(xaxis, mean2, yerr = se2, ecolor = 'b', elinewidth = 2, capsize = 5)
plt.show()
The extra thin line is coming from the errorbar() call.
errorbar will draw a line too, what you're doing is changing the colour of the error bars, but not the actual lines (hence it using the standard matplotlib first two colours, blue and green.
it's all in the documentaion, here.
To achieve what you want, you only need to use the errorbar() function;
This does what you want i think, maybe jsut tweak the numbers a bit.
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
xaxis = [1, 2, 3]
mean1 = [1,2,3.6]
se1 = [0.2, 0.5, 0.9]
mean2 = [10, 29, 14]
se2 = [3, 4, 2]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('X', fontsize = 16)
ax.set_ylabel('Y', fontsize = 16)
ax.axis([0, 5, 0, 35])
linestyle = {"linestyle":"--", "linewidth":4, "markeredgewidth":5, "elinewidth":5, "capsize":10}
ax.errorbar(xaxis, mean1, yerr = se1, color="r", **linestyle)
ax.errorbar(xaxis, mean2, yerr = se2, color="b", **linestyle)
plt.show()
I put the common line style arguments into a dict which gets unpacked.

Categories