Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change x-axis ticks to custom strings

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()
like image 847
Stephano Avatar asked Apr 28 '17 06:04

Stephano


People also ask

How do you set the X-axis ticks?

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.

How do I create a custom tick in MatPlotLib?

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.


2 Answers

New in matplotlib 3.5.0

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.

like image 55
tdy Avatar answered Oct 20 '22 01:10

tdy


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()

Minimal example

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:

Plot with custom x-axis ticklabels

like image 36
Michael Gecht Avatar answered Oct 20 '22 00:10

Michael Gecht