Home > Net >  Setting x and y limits not working in a figure I want to recreate
Setting x and y limits not working in a figure I want to recreate

Time:05-25

I want to make a figure like this

enter image description here

The problem is that when I set xticks and yticks nothing happens. I have the code below

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.mpl.ticker as cmt
import xarray as xr
import matplotlib.ticker as ticker
from shapely import vectorized
import pandas as pd
import csv
# Open file

grl = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/OUTPUTS/output/ens1/0/yelmo1d.nc')
topo = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/Greenland/GRL-16KM/GRL-16KM_TOPO-M17.nc')

#My data
A_ice = grl["A_ice"][0]
V_ice = grl["V_ice"][0]

# Data points from goelzer 
points = pd.read_csv("/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/points.csv")
diamonds = pd.read_csv("/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/diamonds.csv")

#Convert to list of tuples
with open('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/points.csv', newline='') as f:
    reader = csv.reader(f)
    points = [tuple(row) for row in reader]

with open('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/diamonds.csv', newline='') as f:
    reader = csv.reader(f)
    diamonds = [tuple(row) for row in reader]
# Now start the plots

# Create a figure
fig = plt.figure()
# Add a subplot
ax = fig.add_subplot()
ax.set_title("Grounded ice area and grounded volume")

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

x_mydata = A_ice
y_mydata = V_ice
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.grid()
plt.plot(x_mydata, y_mydata, marker="^", markersize=20, markeredgecolor="red", markerfacecolor="red")
plt.plot(float(points[0][0]),float(points[0][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[1][0]),float(points[1][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[2][0]),float(points[2][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[3][0]),float(points[3][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[4][0]),float(points[4][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[5][0]),float(points[5][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[6][0]),float(points[6][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[7][0]),float(points[7][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[8][0]),float(points[8][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[9][0]),float(points[9][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[10][0]),float(points[10][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[11][0]),float(points[11][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[12][0]),float(points[12][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[13][0]),float(points[13][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(diamonds[0][0]),float(diamonds[0][1]), marker="D", markersize=14, markeredgecolor="green", markerfacecolor="green")
plt.plot(float(diamonds[1][0]),float(diamonds[1][1]), marker="D", markersize=14, markeredgecolor="green", markerfacecolor="green")
plt.show()

x_axis = np.arange(1.6,2,1)
y_axis = np.arange(1.6,3.3,1)
plt.xticks(x_axis)
plt.xticks(y_axis)


#ax.set_xticks(x_axis)
#ax.set_yticks(y_axis)



This is the resulting figure I get. The points are close by since the axis limits have not been set for some reason to the ones in the figure above.

PS: A_ice and H_ice is my own data point while the csv files points and diamonds are from another dataset

enter image description here

Any help will be much appreciated! Thanks!

CodePudding user response:

Looks like you are calling .show() and then setting the ticks. That could be a problem.

What you really want is matplotlib.pyplot.ylim and xlim. That will control the min and max values of the axes.

The end of the code would look more like:

#plt.show()
#x_axis = np.arange(1.6,2,1)
#y_axis = np.arange(1.6,3.3,1)
plt.xlim(left=0, right=2)
plt.ylim(top=3.2, bottom=0)

plt.show() # move here
  • Related