Как я сериализирую объект в XDocument?

Я генерирую набор данных для контурного графика на карте Cartopy. Точки данных для контурной обработки разделены на 2 группы с отрицательными и положительными значениями z. Numpy maskedarray используется в этой операции. Я надеюсь, что это полезно для общих читателей, в том числе ОП.

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import pandas as pd

from numpy.random import uniform, seed
from matplotlib.mlab import griddata   
# TODO, use newer scipy.interpolate() instead of `griddata`
import numpy.ma as ma

# make up some data around long,lat: (90, 18)
seed(0)
npts = 200
x0, y0 = 90, 18   # center of map in (long, lat), degrees
x = x0+uniform(-2, 2, npts)
y = y0+uniform(-2, 2, npts)
#z = x*np.exp(-x**2 - y**2)
z = (x-x0)*np.exp(-(x-x0)**2 - (y-y0)**2)  # elevation in meters

# define grid, for points interpolation from the made-up data above
gridx, gridy = 50,50
xi = x0+np.linspace(-2.1, 2.1, gridx)
yi = y0+np.linspace(-2.1, 2.1, gridy)

# interpolate for gridded data of (gridx, gridy) 
zi = griddata(x, y, z, xi, yi, interp='linear')
# xi.shape, yi.shape, zi.shape  => ((50,), (50,), (50, 50))

xig,yig = np.meshgrid(xi, yi)

# projection
useproj = ccrs.PlateCarree()
fig = plt.figure(figsize = (9, 7))
rect = [0.05, 0.05, 0.95, 0.95]  # for map extent
ax = fig.add_axes( rect, projection=useproj )

# contour the gridded data, plotting dots at the nonuniform data points.
CS = ax.contour(xig, yig, zi, 15, linewidths=0.5, colors='k')
CS = ax.contourf(xig, yig, zi, 15,
                  vmax=abs(zi).max(), vmin=-abs(zi).max())
plt.colorbar(CS)  # draw colorbar

# prep points for scatterplot of the gridded points
# make 2 masked-arrays, based on `zi`
mag = ma.masked_greater(zi, 0)  # mask points with +ve zi values
mal = ma.masked_less(zi, 0)     # mask points with -ve zi values

# apply masking to xig,yig; borrowing mask from mag
xig_greater_masked = ma.MaskedArray(xig, mask=mag.mask)  # must have compatible values
yig_greater_masked = ma.MaskedArray(yig, mask=mag.mask)

# apply masking to xig,yig; borrowing mask from mal
xig_less_masked = ma.MaskedArray(xig, mask=mal.mask)
yig_less_masked = ma.MaskedArray(yig, mask=mal.mask)

# for points with -ve z values (result of .masked_greater)
plt.scatter(xig_greater_masked, yig_greater_masked, s=3, color="w", \
            alpha=1, zorder=15, label="masked_greater z")
# for points with +ve z values (result of .masked_less)
ax.scatter(xig_less_masked, yig_less_masked, s=3, color="r", alpha=1, \
           zorder=15, label="masked_less z") 

leg = ax.legend(title='Masked z', framealpha=1.0, facecolor="lightgray")
leg.set_zorder(20)

gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=2, color='gray', alpha=0.5, linestyle='--')

gl.xlabels_top = False
gl.ylabels_right = False

gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 15, 'color': 'gray'}
#gl.xlabel_style = {'color': 'gray', 'weight': 'bold'}

plt.title('Masked data plot on contour')
plt.show()

Результирующий сюжет:

enter image description here

19
задан Simon Keep 30 April 2009 в 09:28
поделиться

1 ответ

Вы можете создать XmlWriter непосредственно в XDocument:

XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
    // write xml into the writer
    var serializer = new DataContractSerializer(objectToSerialize.GetType());
    serializer.WriteObject(writer, objectToSerialize);
}
Console.WriteLine(doc.ToString());
44
ответ дан 30 November 2019 в 03:03
поделиться
Другие вопросы по тегам:

Похожие вопросы: