Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib tick axis notation with superscript

I would like to produce some plot over the frequencies. I want to have an x-axis with superscript notation like in here. In addition I need vertical lines with vertical annotation separate kilo and mega Hz.

import numpy as np
import matplotlib.pyplot as plt
band = np.linspace(0,10**12,100)
y = band

plt.plot(band,y)
plt.xlabel('Frequencies')

plt.vlines(10**3, min(y), max(y),colors = 'black', label = 'kilo Hz')
plt.vlines(10**6, min(y), max(y),colors = 'black', label = 'mega Hz')
plt.legend()
plt.show()

I tried use ticker but can't figure it out how to set up it. I tried to follow some examples but got error like AttributeError: 'Figure' object has no attribute 'ticklabel_format' Already spend quite a lot of time on it and don't know how to move forward.

In general I need the x-axis formatted in the similar way, than if plt.xscale('log') but I want to keep linear scale.

like image 641
tomasz74 Avatar asked Feb 13 '26 00:02

tomasz74


1 Answers

You can define the tick-marks as strings and assign those:

mport numpy as np
import matplotlib.pyplot as plt
band = np.linspace(0,10**12,100)
y = band

plt.plot(band,y)
plt.xlabel("Frequencies")

plt.vlines(10**3, min(y), max(y),colors = 'black', label = 'kilo Hz')
plt.vlines(10**6, min(y), max(y),colors = 'black', label = 'mega Hz')

string_labels = []
for i in range(0,len(y),10):
    string_labels.append(r"$10^{%02d}$" % (i/10.0))

plt.xticks(np.linspace(0,10**12,10),string_labels)

plt.legend()
plt.show()
like image 197
Schorsch Avatar answered Feb 14 '26 12:02

Schorsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!