Home > Enterprise >  How to use the object oriented version of set_xticks in Matplotlib?
How to use the object oriented version of set_xticks in Matplotlib?

Time:11-14

Here is this code:

import matplotlib.pyplot as plt
import numpy as np
from numpy.core.function_base import linspace
import pandas as pd

data = {'col1' : linspace(1,3),
        'col2' : linspace(3, 6)
        }
df = pd.DataFrame(data)

fig = plt.figure()
axes = df['col1'].plot()




plt.show()

How can I set 'col2' of the data frame to be the X-axis using the object-oriented approach, please?

Bonus: How to use two X-axis? 'col2' would be the X-axis displayed at the bottom of the plot, 'col3' would be the X-axis marked at the top of the plot? Again, using the object-oriented approach.

CodePudding user response:

For the second x-axis you can go here. For your original question you can use just the following:

import matplotlib.pyplot as plt
import numpy as np
from numpy.core.function_base import linspace
import pandas as pd

data = {'col1' : linspace(1,3),
        'col2' : linspace(3, 6)
        }
df = pd.DataFrame(data)

fig,ax = plt.subplots()
axes = ax.plot(df['col2'],df['col1'])
ax.set_xlabel('col2')
ax.set_ylabel('col1')

plt.show()
  • Related