Writing $sqrt(2)$ instead of 1.414 in a plot legendPython join: why is it string.join(list) instead of list.join(string)?Hiding axis text in matplotlib plotsPlot two graphs in same plot in RHow to put the legend out of the plotWhen to use cla(), clf() or close() for clearing a plot in matplotlib?Save plot to image file instead of displaying it using MatplotlibHow to make IPython notebook matplotlib plot inlineHow to replace matplotlib legend and keep same location?Issue with xticklabels when saving a figure with matplotlibSymbol colour in legend in errorbar plot does not correspond to colour on figure
Why do Ryanair allow me to book connecting itineraries through a third party, but not through their own website?
Website returning plaintext password
Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?
How should I introduce map drawing to my players?
Why does Mjolnir fall down in Age of Ultron but not in Endgame?
Pirate democracy at its finest
Using credit/debit card details vs swiping a card in a payment (credit card) terminal
A steel cutting sword?
Have 1.5% of all nuclear reactors ever built melted down?
Is it possible to play as a necromancer skeleton?
Why were helmets and other body armour not commonplace in the 1800s?
How to use libraries with delays inside within a time critical STM32 HAL application?
Should I disclose a colleague's illness (that I should not know) when others badmouth him
Inconsistent results from Wolfram Could
How to patch glass cuts in a bicycle tire?
In general, would I need to season a meat when making a sauce?
What was the idiom for something that we take without a doubt?
My players want to grind XP but we're using milestone advancement
Is it rude to call a professor by their last name with no prefix in a non-academic setting?
Maxima of Brownian motion
Is DateWithin30Days(Date 1, Date 2) an Apex Method?
Where can I find visible/radio telescopic observations of the center of the Milky Way galaxy?
Plot twist where the antagonist wins
number headings
Writing $sqrt(2)$ instead of 1.414 in a plot legend
Python join: why is it string.join(list) instead of list.join(string)?Hiding axis text in matplotlib plotsPlot two graphs in same plot in RHow to put the legend out of the plotWhen to use cla(), clf() or close() for clearing a plot in matplotlib?Save plot to image file instead of displaying it using MatplotlibHow to make IPython notebook matplotlib plot inlineHow to replace matplotlib legend and keep same location?Issue with xticklabels when saving a figure with matplotlibSymbol colour in legend in errorbar plot does not correspond to colour on figure
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Using matplotlib for Python, how do I write put 'sqrt(2)' with the square root symbol instead of its decimal value in a plot legend? See the plot and code below. Please let me know if I need to elaborate. Thank you!
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
x = np.asarray([2*np.sqrt(2), 2, np.sqrt(3)])
for i in x:
ax.plot(E,T(i,E),label='hkl=%s' % i)
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(1.01, 1), loc=1, borderaxespad=0.)
The plot this spits out is:
EDIT:
I should mention that I want this code to take any value in x
and plot it, using an actual square root symbol in the legend instead of its decimal value.
python matplotlib plot
add a comment |
Using matplotlib for Python, how do I write put 'sqrt(2)' with the square root symbol instead of its decimal value in a plot legend? See the plot and code below. Please let me know if I need to elaborate. Thank you!
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
x = np.asarray([2*np.sqrt(2), 2, np.sqrt(3)])
for i in x:
ax.plot(E,T(i,E),label='hkl=%s' % i)
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(1.01, 1), loc=1, borderaxespad=0.)
The plot this spits out is:
EDIT:
I should mention that I want this code to take any value in x
and plot it, using an actual square root symbol in the legend instead of its decimal value.
python matplotlib plot
add a comment |
Using matplotlib for Python, how do I write put 'sqrt(2)' with the square root symbol instead of its decimal value in a plot legend? See the plot and code below. Please let me know if I need to elaborate. Thank you!
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
x = np.asarray([2*np.sqrt(2), 2, np.sqrt(3)])
for i in x:
ax.plot(E,T(i,E),label='hkl=%s' % i)
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(1.01, 1), loc=1, borderaxespad=0.)
The plot this spits out is:
EDIT:
I should mention that I want this code to take any value in x
and plot it, using an actual square root symbol in the legend instead of its decimal value.
python matplotlib plot
Using matplotlib for Python, how do I write put 'sqrt(2)' with the square root symbol instead of its decimal value in a plot legend? See the plot and code below. Please let me know if I need to elaborate. Thank you!
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
x = np.asarray([2*np.sqrt(2), 2, np.sqrt(3)])
for i in x:
ax.plot(E,T(i,E),label='hkl=%s' % i)
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(1.01, 1), loc=1, borderaxespad=0.)
The plot this spits out is:
EDIT:
I should mention that I want this code to take any value in x
and plot it, using an actual square root symbol in the legend instead of its decimal value.
python matplotlib plot
python matplotlib plot
edited Mar 24 at 4:40
Kosta
asked Mar 24 at 4:25
KostaKosta
1305
1305
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use Unicode to write the square root symbol as a string and then add whatever number you are interested in. To write a square root symbol as a string, use 'u221A'
.
One way to modify your code would be to turn x into a dictionary. So x='2u221A2': 2*np.sqrt(2), '2':2, 'u221A3':np.sqrt(3)
. Your for loop would become for k,v in x.items()
where k and v are short for key and value and the first line in the loop would be ax.plot(E,T(v,E),label='hkl=%s' % k)
In terms of parsing a number and writing it out as a square root, that's a little more involved and would look something like this question on code review: https://codereview.stackexchange.com/questions/144041/reduce-square-root-to-simplest-radical-form
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
add a comment |
You can do the printing of the square root symbol pretty easily using LaTex in maplotlib, i.e.
s = 2
plt.plot(x, y, label=r'$sqrt$'.format(s))
The tricky part of course is using the right coefficients and roots, this is easier if you take a slightly different approach to generating x
, something like this
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
k = [(2, 2), (2, 1), (1, 3)]
x = list()
labels = list()
for i in range(len(k)):
x.append(k[i][0] * np.sqrt(k[i][1]))
if k[i][0] == 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][1]))
elif k[i][0] != 1 and k[i][1] == 1:
labels.append(r'hkl=$$'.format(k[i][0]))
elif k[i][0] != 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][0], k[i][1]))
else:
labels.append('hkl=1')
for i in range(len(x)):
ax.plot(E,T(x[i],E),label=labels[i])
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(0.99, 0.98), loc=1, borderaxespad=0.)
This will take any values to construct x
and print the square root symbols approriately (though I am sure there are better ways to do so)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320722%2fwriting-sqrt2-instead-of-1-414-in-a-plot-legend%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Unicode to write the square root symbol as a string and then add whatever number you are interested in. To write a square root symbol as a string, use 'u221A'
.
One way to modify your code would be to turn x into a dictionary. So x='2u221A2': 2*np.sqrt(2), '2':2, 'u221A3':np.sqrt(3)
. Your for loop would become for k,v in x.items()
where k and v are short for key and value and the first line in the loop would be ax.plot(E,T(v,E),label='hkl=%s' % k)
In terms of parsing a number and writing it out as a square root, that's a little more involved and would look something like this question on code review: https://codereview.stackexchange.com/questions/144041/reduce-square-root-to-simplest-radical-form
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
add a comment |
You can use Unicode to write the square root symbol as a string and then add whatever number you are interested in. To write a square root symbol as a string, use 'u221A'
.
One way to modify your code would be to turn x into a dictionary. So x='2u221A2': 2*np.sqrt(2), '2':2, 'u221A3':np.sqrt(3)
. Your for loop would become for k,v in x.items()
where k and v are short for key and value and the first line in the loop would be ax.plot(E,T(v,E),label='hkl=%s' % k)
In terms of parsing a number and writing it out as a square root, that's a little more involved and would look something like this question on code review: https://codereview.stackexchange.com/questions/144041/reduce-square-root-to-simplest-radical-form
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
add a comment |
You can use Unicode to write the square root symbol as a string and then add whatever number you are interested in. To write a square root symbol as a string, use 'u221A'
.
One way to modify your code would be to turn x into a dictionary. So x='2u221A2': 2*np.sqrt(2), '2':2, 'u221A3':np.sqrt(3)
. Your for loop would become for k,v in x.items()
where k and v are short for key and value and the first line in the loop would be ax.plot(E,T(v,E),label='hkl=%s' % k)
In terms of parsing a number and writing it out as a square root, that's a little more involved and would look something like this question on code review: https://codereview.stackexchange.com/questions/144041/reduce-square-root-to-simplest-radical-form
You can use Unicode to write the square root symbol as a string and then add whatever number you are interested in. To write a square root symbol as a string, use 'u221A'
.
One way to modify your code would be to turn x into a dictionary. So x='2u221A2': 2*np.sqrt(2), '2':2, 'u221A3':np.sqrt(3)
. Your for loop would become for k,v in x.items()
where k and v are short for key and value and the first line in the loop would be ax.plot(E,T(v,E),label='hkl=%s' % k)
In terms of parsing a number and writing it out as a square root, that's a little more involved and would look something like this question on code review: https://codereview.stackexchange.com/questions/144041/reduce-square-root-to-simplest-radical-form
edited Mar 24 at 5:01
answered Mar 24 at 4:33
TyberiusTyberius
149111
149111
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
add a comment |
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
Can you show me how to incorporate this into my code? I'm still learning Python and could use an example of this.
– Kosta
Mar 24 at 4:38
add a comment |
You can do the printing of the square root symbol pretty easily using LaTex in maplotlib, i.e.
s = 2
plt.plot(x, y, label=r'$sqrt$'.format(s))
The tricky part of course is using the right coefficients and roots, this is easier if you take a slightly different approach to generating x
, something like this
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
k = [(2, 2), (2, 1), (1, 3)]
x = list()
labels = list()
for i in range(len(k)):
x.append(k[i][0] * np.sqrt(k[i][1]))
if k[i][0] == 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][1]))
elif k[i][0] != 1 and k[i][1] == 1:
labels.append(r'hkl=$$'.format(k[i][0]))
elif k[i][0] != 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][0], k[i][1]))
else:
labels.append('hkl=1')
for i in range(len(x)):
ax.plot(E,T(x[i],E),label=labels[i])
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(0.99, 0.98), loc=1, borderaxespad=0.)
This will take any values to construct x
and print the square root symbols approriately (though I am sure there are better ways to do so)
add a comment |
You can do the printing of the square root symbol pretty easily using LaTex in maplotlib, i.e.
s = 2
plt.plot(x, y, label=r'$sqrt$'.format(s))
The tricky part of course is using the right coefficients and roots, this is easier if you take a slightly different approach to generating x
, something like this
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
k = [(2, 2), (2, 1), (1, 3)]
x = list()
labels = list()
for i in range(len(k)):
x.append(k[i][0] * np.sqrt(k[i][1]))
if k[i][0] == 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][1]))
elif k[i][0] != 1 and k[i][1] == 1:
labels.append(r'hkl=$$'.format(k[i][0]))
elif k[i][0] != 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][0], k[i][1]))
else:
labels.append('hkl=1')
for i in range(len(x)):
ax.plot(E,T(x[i],E),label=labels[i])
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(0.99, 0.98), loc=1, borderaxespad=0.)
This will take any values to construct x
and print the square root symbols approriately (though I am sure there are better ways to do so)
add a comment |
You can do the printing of the square root symbol pretty easily using LaTex in maplotlib, i.e.
s = 2
plt.plot(x, y, label=r'$sqrt$'.format(s))
The tricky part of course is using the right coefficients and roots, this is easier if you take a slightly different approach to generating x
, something like this
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
k = [(2, 2), (2, 1), (1, 3)]
x = list()
labels = list()
for i in range(len(k)):
x.append(k[i][0] * np.sqrt(k[i][1]))
if k[i][0] == 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][1]))
elif k[i][0] != 1 and k[i][1] == 1:
labels.append(r'hkl=$$'.format(k[i][0]))
elif k[i][0] != 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][0], k[i][1]))
else:
labels.append('hkl=1')
for i in range(len(x)):
ax.plot(E,T(x[i],E),label=labels[i])
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(0.99, 0.98), loc=1, borderaxespad=0.)
This will take any values to construct x
and print the square root symbols approriately (though I am sure there are better ways to do so)
You can do the printing of the square root symbol pretty easily using LaTex in maplotlib, i.e.
s = 2
plt.plot(x, y, label=r'$sqrt$'.format(s))
The tricky part of course is using the right coefficients and roots, this is easier if you take a slightly different approach to generating x
, something like this
import matplotlib.pyplot as plt
import numpy as np
E = np.linspace(50000,200000,1000) # eV
a = 3.615*10**(-10) #m
h = 6.582*10**(-16) #eV s
m = 9.109*10**(-31) #kg
J = 6.242*10**(-18) #1eV to J
def T(x,E):
return np.arcsin(x*np.sqrt(J*((np.pi**2 *h**2)/(a**2 * 4*m *E))))*1000
fig, ax = plt.subplots()
k = [(2, 2), (2, 1), (1, 3)]
x = list()
labels = list()
for i in range(len(k)):
x.append(k[i][0] * np.sqrt(k[i][1]))
if k[i][0] == 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][1]))
elif k[i][0] != 1 and k[i][1] == 1:
labels.append(r'hkl=$$'.format(k[i][0]))
elif k[i][0] != 1 and k[i][1] != 1:
labels.append(r'hkl=$sqrt$'.format(k[i][0], k[i][1]))
else:
labels.append('hkl=1')
for i in range(len(x)):
ax.plot(E,T(x[i],E),label=labels[i])
plt.ylabel('$Theta$ (mrad)')
plt.xlabel('Energy (eV)')
plt.legend(bbox_to_anchor=(0.99, 0.98), loc=1, borderaxespad=0.)
This will take any values to construct x
and print the square root symbols approriately (though I am sure there are better ways to do so)
edited Mar 24 at 6:43
answered Mar 24 at 6:13
William MillerWilliam Miller
1,645318
1,645318
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320722%2fwriting-sqrt2-instead-of-1-414-in-a-plot-legend%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown