I have the following code:
x1 = np.linspace(0, 5, 10)
y1 = x1 + np.random.randn(10)
y2 = x1 + np.random.randn(10)
x2 = np.linspace(0, 5, 10)
y3 = x2 + np.random.randn(10)
y4 = x2 + np.random.randn(10)
x3 = np.linspace(0, 5, 10)
y5 = x3 + np.random.randn(10)
y6 = x3 + np.random.randn(10)
x4 = np.linspace(0, 5, 10)
y7 = x4 + np.random.randn(10)
y8 = x4 + np.random.randn(10)
# Set up a figure with 2 rows and 2 columns of subplots
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
x = [i for i in range(10,101,10)]
# Plot multiple line charts on each subplot
l1, = ax1.plot(x1, y1, 'o-')
l2, = ax1.plot(x1, y2, '^-')
ax1.legend((l1, l2), ('Line 1', 'Line 2'))
l3, = ax2.plot(x2, y3, 's-')
l4, = ax2.plot(x2, y4, 'd-')
ax2.legend((l3, l4), ('Line 3', 'Line 4'))
l5, = ax3.plot(x3, y5, 'x-')
l6, = ax3.plot(x3, y6, '+-')
ax3.legend((l5, l6), ('Line 5', 'Line 6'))
l7, = ax4.plot(x4, y7, '*-')
l8, = ax4.plot(x4, y8, 'p-')
ax4.legend((l7, l8), ('Line 7', 'Line 8'))
# Set the x- and y-limits and labels for each subplot
ax1.set_xlim([0, 5])
ax1.set_ylim([0, 10])
ax1.set_xlabel('X1')
ax1.set_ylabel('Y1')
ax1.set_title('Subplot 1')
ax2.set_xlim([0, 5])
ax2.set_ylim([0, 10])
ax2.set_xlabel('X2')
ax2.set_ylabel('Y2')
ax2.set_title('Subplot 2')
ax3.set_xlim([0, 5])
ax3.set_ylim([0, 10])
ax3.set_xlabel('X3')
ax3.set_ylabel('Y3')
Which yields:
With my actual datasets, the lines with the same color are the same. Hence, I only need one legend on the side of the four subplots. How can I do this?
Related
I need to fill in the shaded area between my graphs.What should I write in fill_between to do this?
import numpy as np
import matplotlib.pyplot as plt
y = lambda z: (2 * z - z ** 2) ** (1 / 2)
y1 = lambda x: (6 * x - x ** 2) ** (1 / 2)
y2 = lambda c: c
x = np.linspace(0, 12, 500)
z = np.linspace(0, 12, 500)
c = np.linspace(0, 12, 500)
plt.ylim(0, 4)
plt.xlim(0, 4)
plt.plot(z, y(z), color='blue', label="$y=\\sqrt{2x-x^2}$")
plt.plot(c, y2(c), color='black', label='$y=x$')
plt.plot(x, y1(x), color='red', label='$y=\\sqrt{6x-x^2}$')
plt.plot([0, 4], [0, 0], color='yellow', label='y=0')
plt.grid(True, zorder=5)
miny = np.minimum(y2(c), y1(x))
plt.fill_between(x, y(z), miny, where=(miny > y(x)), alpha=0.5)
plt.legend()
plt.show()
Make the domain of your functions to be the same (e.g. [0, 4]). The below code does what you want:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 500)
y = np.piecewise(x, [x <= 2, x > 2], [lambda x: np.sqrt(2 * x - x ** 2), 0])
y1 = np.sqrt(6 * x - x ** 2)
y2 = x
y3 = 0*x
plt.plot(x, y, color='blue', label="$y=\\sqrt{2x-x^2}$")
plt.plot(x, y1, color='red', label='$y1=\\sqrt{6x-x^2}$')
plt.plot(x, y2, color='black', label='$y2=x$')
plt.plot(x, y3, color='yellow', label='y3=0')
plt.grid(True, zorder=5)
miny = np.minimum(y2, y1)
plt.fill_between(x, y, miny, where = (miny > y), alpha=0.5)
plt.legend()
plt.show()
I have 3 curves and I want to fill the area between them. How should I do this?
This is what I have so far:
import numpy as np
import matplotlib.pyplot as plt
y = lambda z: -(z ** 2)
y1 = lambda x: x ** (1 / 3)
x = np.linspace(0, 2, 100)
z = np.linspace(0, 2, 100)
plt.plot(z, y(z), color='blue', label="y=-(x^2)")
plt.ylim(-2, 2)
plt.xlim(0, 2)
plt.plot(x, y1(x), color='red', label='y=x^(1/3)')
plt.plot([1, 1, 1], [0, -2, 2], color='black', label='x=1')
plt.grid(True, zorder=5)
plt.legend()
k = np.arange(0,2)
f = [0,-0.2]
p = [0,0.2]
plt.fill_between(k,f,p,interpolate=True)
plt.show()
You can use where in fill_between to take care of x = 1 line. See below:
import numpy as np
import matplotlib.pyplot as plt
y = lambda z: -(z ** 2)
y1 = lambda x: x ** (1 / 3)
x = np.linspace(0, 2, 100)
z = np.linspace(0, 2, 100)
plt.ylim(-2, 2)
plt.xlim(0, 2)
#plt.grid(True, zorder=5)
plt.plot(z, y(z), color='blue', label="y=-(x^2)")
plt.plot(x, y1(x), color='red', label='y=x^(1/3)')
plt.plot([1, 1, 1], [0, -2, 2], color='black', label='x=1')
plt.fill_between(x, y(z), y1(x), where=x<=1)
plt.legend()
plt.show()
I'm trying to plot the surfaces of a polygon generated from arrays of x, y, z
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
print("numpy version: " + np.__version__)
# [x, y, z] coordinates
p1 = [322697.1875, 3663966.5, -30000.0]
p2 = [325054.34375, 3663966.5, -30000.0]
p3 = [325054.34375, 3665679.5, -30000.0]
p4 = [322697.1875, 3665679.5, -30000.0]
p5 = [322697.1875, 3663966.5, -27703.123046875]
p6 = [325054.34375, 3663966.5, -27703.154296875]
p7 = [325054.34375, 3665679.5, -27703.70703125]
p8 = [322697.1875, 3665679.5, -27703.673828125]
points = [p1, p2, p3, p4, p5, p6, p7, p8]
points = np.array(points)
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
X, Y = np.meshgrid(x, y)
zr = np.tile(z, [8, 1])
fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection = '3d')
ax.plot_surface(X, Y, zr, alpha=0.5)
plt.show()
Here is the output
I would like the output to show each side of the polygon as shaded. What am I doing wrong?
Here you go
=^..^=
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# setup data
p0 = [0, 0, 0]
p1 = [1, 1, 1]
p2 = [2, 2, 2]
p3 = [3, 3, 3]
p4 = [4, 4, 4]
p5 = [5, 5, 5]
p6 = [6, 6, 6]
p7 = [7, 7, 7]
p8 = [8, 8, 8]
# create data array
points = [p0, p1, p2, p3, p4, p5, p6, p7, p8]
points = np.array(points)
# get array co-ordinates
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
# create mesh for X and Y set points
X, Y = np.meshgrid(x, y)
# create 0 surface
Z1 = np.zeros_like(X)
# create 8 surface
Z2 = np.full_like(X, 8)
# plot data
fig = plt.figure(figsize=(16, 10))
ax = fig.gca(projection='3d')
# setup each surface
ax.plot_surface(X, Y, Z1, alpha=0.3)
ax.plot_surface(X, Z1, Y, alpha=0.3)
ax.plot_surface(Z1, X, Y, alpha=0.3)
ax.plot_surface(X, Y, Z2, alpha=0.3)
ax.plot_surface(X, Z2, Y, alpha=0.3)
ax.plot_surface(Z2, X, Y, alpha=0.3)
plt.show()
Output:
I want to turn a normal plot into a subplot. Here's the code for the plot, which works:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
d = {'A': [1, 2, 3, 4, 5, 6], 'B': [-2.5, -1.00, .25, 1.56, .75, 1.20]}
df = pd.DataFrame(data=d)
x = np.arange(0, 999, 0.1)
y1 = -.75
y2 = .75
plt.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
plt.scatter(df.A, df.B)
plt.plot(df.A, df.B)
plt.axhline(y=0, color='black')
plt.xticks(np.arange(0, 999))
plt.ylim([-4, 4])
plt.xlim([0, df.A.max() + 1])
plt.show()
Then here's what I tried to make it into a subplot. The console doesn't throw any errors, it's just not showing any plot.
fig = Figure()
ax = fig.add_subplot(111)
x = np.arange(0, 999, 0.1)
y1 = -.75
y2 = .75
ax.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
ax.scatter(df.A, df.B)
ax.plot(df.A, df.B)
ax.axhline(y=0, color='black')
ax.set_xticks(np.arange(0, 999))
ax.set_ylim([-4, 4])
ax.set_xlim([0, df.A.max() + 1])
plt.show()
What am I doing wrong?
Use fig = plt.figure() instead of fig = Figure().
Your code would be:
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(0, 999, 0.1)
y1 = -.75
y2 = .75
ax.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
ax.scatter(df.A, df.B)
ax.plot(df.A, df.B)
ax.axhline(y=0, color='black')
ax.set_xticks(np.arange(0, 999))
ax.set_ylim([-4, 4])
ax.set_xlim([0, df.A.max() + 1])
plt.show()
Output:
i would like to multiple stacked bar in the same plot. This is my code:
file_to_plot = file_to_plot.set_index(['user'])
fig, ax = plt.subplots()
fontP = FontProperties()
fontP.set_size('small')
file_to_plot[[" mean_accuracy_all_classes_normal", " delta_all_classes"]].plot(ax=ax, kind='bar', color= ['g', 'r'], width = 0.65, align="center", stacked=True)
file_to_plot[[" mean_accuracy_user_classes_normal", " delta_user_classes"]].plot(ax=ax, kind='bar', color=['y', 'b'], width=0.65, align="center", stacked = True)
lgd = ax.legend(['Tutte le classi (normale)', 'Tutte le classi (incrementale)', 'Classi utente (normale)', 'Classi utente (incrementale)'], prop=fontP, loc=9, bbox_to_anchor=(0.5, -0.15), ncol=4,borderaxespad=0.)
ax.set_ylabel('% Accuratezza')
ax.set_xlabel('Utenti')
This is the results:
The second plot overwhelms me when I want to plot them together. How can I do?
This should work the way you want:
import pandas as pd
df = pd.DataFrame(dict(
A=[1, 2, 3, 4],
B=[2, 3, 4, 5],
C=[3, 4, 5, 6],
D=[4, 5, 6, 7]))
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(20, 10))
ab_bar_list = [plt.bar([0, 1, 2, 3], df.B, align='edge', width= 0.2),
plt.bar([0, 1, 2, 3], df.A, align='edge', width= 0.2)]
cd_bar_list = [plt.bar([0, 1, 2, 3], df.D, align='edge',width= -0.2),
plt.bar([0, 1, 2, 3], df.C, align='edge',width= -0.2)]
Just keep in mind, the width value for one group must be positive, and negative for the second one. Use align by edge as well.
You have to place the bar with the biggest values before the bar with the lowest values, and if you want the bars to appear stacked above one another rather than one in front of another, change df.B and df.D to df.B + df.A and df.D + df.C, respectively. If there's no apparent or consisting pattern, use the align by edge and width method with the one suggested by #piRSquared.
Another alternative would be to access each value from a green bar and compare it to the corresponding value from the red bar, and plot accordingly (too much unnecessary work in this one).
I thought this would be straightforward. Hopefully someone else will chime in with a better solution. What I did was to take the diff's of the columns and run a stacked chart.
df = pd.DataFrame(dict(
A=[1, 2, 3, 4],
B=[2, 3, 4, 5],
C=[3, 4, 5, 6]
))
df.diff(axis=1).fillna(df).astype(df.dtypes).plot.bar(stacked=True)
For comparison
fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True)
df.plot.bar(ax=axes[0])
df.diff(axis=1).fillna(df).astype(df.dtypes).plot.bar(ax=axes[1], stacked=True)
there is in fact a direct way of stacking the bars via the bottom keyword
(if you plot a horizontal barplot with plt.barh use left instead of bottom)!
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(dict(A=[1, 2, 3, 4], B=[2, 3, 4, 5], C=[3, 4, 5, 6]))
df2 = df / 2
f, ax = plt.subplots()
ax.bar(df.index, df.A, align='edge', width=0.2)
ax.bar(df.index, df.B, align='edge', width=0.2, bottom=df.A)
ax.bar(df.index, df.C, align='edge', width=0.2, bottom=df.A + df.B)
ax.bar(df2.index, df2.A, align='edge', width=-0.2)
ax.bar(df2.index, df2.B, align='edge', width=-0.2, bottom=df2.A)
ax.bar(df2.index, df2.C, align='edge', width=-0.2, bottom=df2.A + df2.B)
I used numpy to add the arrays together. Not sure if its exactly what you wanted, but its what I needed when I stumbled on this question. Thought it might help others.
import matplotlib.pyplot as plt
import numpy as np
dates = ['22/10/21', '23/10/21', '24/10/21', '25/10/21', '26/10/21']
z1 = np.array([20, 35, 30, 35, 27])
z2 = np.array([25, 32, 34, 20, 25])
z3 = np.array([20, 35, 30, 35, 27])
z4 = np.array([25, 32, 34, 20, 25])
z5 = np.array([20, 35, 30, 35, 27])
width = 0.35 # the width of the bars: can also be len(x) sequence
fig, ax = plt.subplots()
ax.bar(dates, z1, width, color='0.8', label='Z1')
ax.bar(dates, z2, width, color='b', label='Z2',bottom=z1)
ax.bar(dates, z3, width, color='g', label='Z3',bottom=z1 + z2)
ax.bar(dates, z4, width, color='tab:orange', label='Z4',bottom=z1 + z2 + z3)
ax.bar(dates, z5, width, color='r', bottom=z1 + z2 + z3 + z4,
label='Z5')
ax.set_ylabel('Time in HR Zones')
ax.set_title('HR Zones')
ax.legend()
plt.show()
Stacked Bar Graph