I am drawing a line chart using matplotlib as shown below.
import matplotlib.pyplot as plt
import pandas as pd
import io
temp = u"""
tenor,yield
1M,5.381
3M,5.451
6M,5.505
1Y,5.393
5Y,4.255
10Y,4.109
"""
data = pd.read_csv(io.StringIO(temp), sep=",")
plt.plot(data['tenor'], data['yield'])
Output: The tick intervals on the x-axis are all the same.
What I want : Set the tick interval of the x-axis differently as shown in the screen below
Is there any way to set tick intvel differently?
In the column
'tenor'
,'M'
represents month and'Y'
represents year. Create a'Month'
column with'Y'
scaled by 12 Months.It's more concise to plot the
data
directly withpandas.DataFrame.plot
, and use.set_xticks
to change the xtick-labels.Tested in
python 3.12.0
,pandas 2.1.1
,matplotlib 3.8.0
The output:
.apply
with alambda
function is fastestCompare Implementations with
%timeit
in JupyterLab.apply
&lambda
.apply
with a function callnp.where
withassignment expression
np.where
withoutassignment expression