'How to fill graph in empty window with matplotlib
This code only gives an empty window. I try to plot a graph with two lines. Is there any way to attach the graph to the window? Thanks!
import matplotlib.pyplot as plt
import numpy as np
alpha =np.deg2rad(np.linspace(0,180,1000))
sigma_1=190 #kPa
sigma_3=145 #kPa
sigma_delta=(sigma_1+sigma_3)/2 + ( (sigma_1-sigma_3)/2 ) * np.cos(2*alpha) #kPa
def sigma_alpha(s1,s3,a):
return ( (s1+s3)/2 + ( (s1-s3)/2 ) * np.cos(2*alpha) )
def tau_alpha(s1,s3,a):
return (( (s1-s3)/2 )*np.sin(2*alpha) )
plt.figure(figsize=(18,10))
plt.plot(alpha, sigma_alpha(sigma_1, sigma_3, alpha), label=r'$sigma_{$alpha$}$', linewidth=3, color='green')
plt.plot(alpha, tau_alpha(sigma_1, sigma_3, alpha), label = r'$tau_{$alpha$}$', linewidth=3, color='red')
plt.xlabel('Angle $\alpha$ [u"\N{DEGREE SIGN}"]')
plt.ylabel('Spenning[kPa]')
plt.title('Spenningsvariasjon for plan $alpha$ = 0-180')
plt.legend(loc=5, framealpha=1, prop={'size':18})
plt.grid()
plt.minorticks_on()
plt.grid(b=None, which='both', axis='x', linewidth=0.5, linestyle='-')
plt.grid(b=None, which='both', axis='y', linewidth=0.5, linestyle='-')
plt.show()
Solution 1:[1]
You get an empty plot due to an error in your 'plt.xlabel' line. The special sign $ is causing the problem. If you read it as a raw string, it should be fixed:
import matplotlib.pyplot as plt
import numpy as np
alpha =np.deg2rad(np.linspace(0,180,1000))
sigma_1=190 #kPa
sigma_3=145 #kPa
sigma_delta=(sigma_1+sigma_3)/2 + ( (sigma_1-sigma_3)/2 ) * np.cos(2*alpha) #kPa
def sigma_alpha(s1,s3,a):
return ( (s1+s3)/2 + ( (s1-s3)/2 ) * np.cos(2*alpha) )
def tau_alpha(s1,s3,a):
return (( (s1-s3)/2 )*np.sin(2*alpha) )
plt.figure(figsize=(18,10))
plt.plot(alpha, sigma_alpha(sigma_1, sigma_3, alpha), label=r'$sigma_{$alpha$}$', linewidth=3, color='green')
plt.plot(alpha, tau_alpha(sigma_1, sigma_3, alpha), label = r'$tau_{$alpha$}$', linewidth=3, color='red')
plt.xlabel(r'Angle $\alpha$ [\circ]$')
plt.ylabel('Spenning[kPa]')
plt.title('Spenningsvariasjon for plan $alpha$ = 0-180')
plt.legend(loc=5, framealpha=1, prop={'size':18})
plt.grid()
plt.minorticks_on()
plt.grid(b=None, which='both', axis='x', linewidth=0.5, linestyle='-')
plt.grid(b=None, which='both', axis='y', linewidth=0.5, linestyle='-')
plt.show()
Note that I've changed N_DEGREES to \circ to obtain the angle character in your label.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
