I want to change the x-axis ticklabels to custom strings, but the following does not work. How can I set the ticklabels to ["one", "two", "three"]
?
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
def pushButtonClicked(self):
code = self.lineEdit.text()
x=["one","two","three"]
l=[1,2,3]
y=[2,3,4]
ax = self.fig.add_subplot(111)
print(1)
ax.plot(l, y, label='DeadPopulation')
ax.xticks(l,x)
print(IntroUI.g_sortArrayDeadcnt)
ax.legend(loc='upper right')
ax.grid()
self.canvas.draw()
xticks( ticks ) sets the x-axis tick values, which are the locations along the x-axis where the tick marks appear. Specify ticks as a vector of increasing values; for example, [0 2 4 6] . This command affects the current axes. xt = xticks returns the current x-axis tick values as a vector.
Set the figure size and adjust the padding between and around the subplots. Create lists for height, bars and y_pos data points. Make a bar plot using bar() method. To customize X-axis ticks, we can use tick_params() method, with color=red, direction=outward, length=7, and width=2.
Use the labels
param of ax.set_xticks
to set ticks and labels simultaneously:
x = [1, 2, 3]
y = [10, 0, 4]
l = ['one', 'two', 'three']
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks(x, labels=l)
# ^^^^^^
Note that plt.xticks
has always had this functionality:
plt.plot(x, y)
plt.xticks(x, labels=l)
Using ax.set_xticklabels
is now discouraged, but the method will remain for backward compatibility.
I assume you just want to set the ticks to be equal to ['one', 'two', 'three']
?
To do this, you need to use set_xticks()
and set_xticklabels()
:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
def pushButtonClicked(self):
code = self.lineEdit.text()
x=["one","two","three"]
l=[1,2,3]
y=[2,3,4]
ax = self.fig.add_subplot(111)
print(1)
ax.plot(l, y, label='DeadPopulation')
# Set the tick positions
ax.set_xticks(l)
# Set the tick labels
ax.set_xticklabels(x)
print(IntroUI.g_sortArrayDeadcnt)
ax.legend(loc='upper right')
ax.grid()
self.canvas.draw()
import matplotlib.pyplot as plt
f, ax = plt.subplots()
x = ['one', 'two', 'three']
l = [1, 2, 3]
y = [2, 3, 4]
ax.plot(l,y)
ax.set_xticks(l)
ax.set_xticklabels(x)
plt.show()
Here is how it would look like:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With