Can I convert a contour plot in matplotlib to a format that can be imported and edited in AutoCAD? - python-3.x

I have written a script in Python 3.3 which reads position (x, y coordinate) and risk values from a text file and saves the resulting contour plot using matplotlib. My company needs to be able to edit the contour in AutoCAD. Unfortunately, my knowledge about AutoCAD is severely limited, and the people at my company who know AutoCAD know very little about generating contour plots.
How can I create a contour plot that can be imported in AutoCAD? My current thinking is that I should save the plot as an svg file and convert it to something AutoCAD can open, or install a plugin for AutoCAD that will allow it to open one of the formats that matplotlib can save. I have seen this question but that does not quite suit my needs.
*Edit*
I have tried saving the plot as an SVG file, opening it in Inkscape, and saving it as DXF, but it does not save the contour color information, and the task needs to be automated anyway. The contour color information is important to preserve because the color indicates the order of magnitude of risk.

If you can generate a postscript file (matplotlib can create pdf's right?) you might be able to use pstoedit from the command line to convert it to a dxf.
Alternatively, you can use Illustrator(not free) or Inkscape(free) to convert the svg to a dxf. There are some general rumblings on the internet that Inkscape will sometime turn bezier curves into straight lines, but I haven't check to see if this is still true or not.

I ended up having my plotting program create a very basic Autocad script. I referred to this question about extracting x,y data from a contour plot to write the Autocad script. Here is the relevant function:
def make_autocad_script(outfile_name, contour):
'''
Creates an Autocad script which contains polylines for each contour.
Args
outfile_name: the name of the Autocad script file.
contour: the contour plot that needs to be exported to Autocad.
'''
with open(outfile_name, 'w', newline='') as outfile:
writer = csv.writer(outfile, delimiter=',', )
# each collection is associated with a contour level
for collection in contour.collections:
# If the contour level is never reached, then the collection will be an empty list.
if collection:
# Set color for contour level
outfile.write('COLOR {}\n'.format(random.randint(1,100)))
# Each continuous contour line in a collection is a path.
for path in collection.get_paths():
vertices = path.vertices
# pline is an autocad command for polyline. It interprets
# the next (x,y) pairs as coordinates of a line until
# it sees a blank line.
outfile.write('pline\n')
writer.writerows(vertices)
outfile.write('\n')
I send make_autocad_script the outfile and contour plot that I need, and in Autocad I import the script. This plots each contour as a random color, but that could be replaced with whatever color you wanted.

Related

Separate file plots from different data files gnuplot

I have 3000 .dat files which I want to plot using gnuplot.
They are all named as "iteration_1", ...", iteration_93", ..."iteration_1247",... (not in the format "iteration_XXXX", if this information is helpful).
Each of those files is to be plotted in an .eps file - my final intention is to do a video (an evolution of those plots), which I can easily make if I have the .eps files.
Is there any way to quickly command gnuplot to do this? All the questions I have found remotely similar to my situation were all actually regarding putting data from different files into one plot in a single file.
Again, I do not want to put all the plots into a single .eps file. I want 3000 .eps files.
Thanks in advance!
Simply put your plotting routine in a do for loop. By the way, gnuplot can also do animated GIFs. Check help gif.
### create output files in a loop
reset session
set terminal epscairo
do for [i=1:3000] {
FILE = sprintf("iteration_%d",i)
set output FILE.".eps"
plot FILE.".dat" u 1:2 w l # or change your extension and plot command accordingly
}
set output
### end of code

"pixel dimension information" when plotting with image in gnuplot

Using gnuplot v5 patch 6 on windows 10 (wxt terminal)
I have a data file of 2D vectors arranged in six columns (x, y, v_x, v_y, v_mag, rho) that I'm trying to plot as a heatmap of v_mag against x and y. The plot generates fine, but it's always coming up with
"No dimension information for 80000 pixels total. Try 200 x 400"
But I have no idea where to specify this in the terminal.
I realise that I can use pm3d map for this, but this doesn't work without setting dgrid3d and that causes problems with plotting dots on top of the heatmap which I'm also doing. I also don't want to generate a matrix file just for the image plot since I need the vector data for analysis later.
In terms of an example, the plot will generate if I literally just write:
plot 'vectors.dat' using 1:2:5 with image, 'dots.dat' with dots
EDIT: added 'set pm3d' to example code
EDIT: example is now minimal code to produce desired plot
EDIT: example data file can be found here
Any help would be appreciated.
After updating my gnuplot to v5.2, this problem has disappeared.

Can I plot some points over an image?

I wanted to get a scatter plot but the constraint is to plot these points over an image with same dimension as the range of points. Is is possible? If yes which library is good to start with? I tried using gnuplot but it is causing problems. For now I have a code which was stated here but didn't work. I tried using a bitmap image with the points to be plotted in data1.dat file and used the gnuplot script which is stated in the link
set terminal pngcairo transparent
set output 'Figure1.bmp'
plot "Co_ordinates0.dat"
set output

How to read a map into octave

This is a follow up to my post three weeks ago here How do I use m_map in octave, without really being a nuissance to kind and busy people. My problem is simply how does one overlay a basemap on an octave contour plot. After interpolating my irregularly spaced data (works for both contour lines and filled contours) I plot with the code:
contour(xi, yi, obsi, cstart:cstep:cend)
colorbar;
xlabel('Longitude'),ylabel('Latitude')
title('Mean Rain Onset')
saveas(gcf,'rainzam.pdf')
And I get
I have downloaded several map formats: ne_50m_admin_0_countries.zip, the apparently obsolete m_map (with associated tbase.Z, gshhg-bin-2.3.2.zip), soa.7z, world-bounds.7z, gshhg-gmt-2.3.2.tar.gz, dcw-gmt-1.1.1.tar.gz.
My question is has anyone used any of these maps in octave or gnuplot, and how to? I would appreciate any assistance.
Basically you have to load those maps in octave, they represent borders or coastlines with two variables (x,y) which you can then add to your plot with
hold on
plot(x,y)
That's the easy part, the hard part is to load the maps. All of them have different formats, which means it is a completely different story how to load them. For instance, the ne_50m_admin_0_countries.zip has a dbf format. Either you convert it first to ascii text and load it easily with the load function of octave or you need the OI package (http://wiki.octave.org/IO_package), which in turn demands java (http://wiki.octave.org/Java_package). I don't think this is the easy way for a newbie, so I suggest to convert the maps individually to text: google for "convert dbf to csv", "convert dbf to text", "convert dbf to ascii", etc... Perhaps some of those maps can be even loaded with excel and then saved as text (csv), the important issue is to convert them to text!
If you want to draw physical coastlines, you may download them from this link
https://www.naturalearthdata.com/downloads/
Then, after the drawing of a contour map of your own datas, you may add the coastlines using the following commands:
pkg load mapping
hold on
h = shapedraw ('FileName.shp','r','linewidth',1)

how to draw objects (rect) from a file

In gnuplot i can draw a rectangle via
set object rect from x0,y0 to x1,y1
How to read the coordinates x0,x1,y0,y1 from a file?
One way would be to put the line of code that sets the rectangle into a separate file and call that file from within the gnuplot script. So you have a file called "coord.txt" that contains the one line
set object rect from 2,2 to 4,40
and you have a gnuplot script called "rect.gp" that says
set title "call rectangle coordinates"
load "coord.txt"
plot x**2
If you now from within gnuplot type load "rect.gp" you get your graph with the rectangle.
That may not be exactly what you are looking for but maybe a first step.
You probably need to store the data from the file into variables, then use these variables to define the locations of objects. The way to do this is not straight as far as I know. Please refer to here. Good luck!

Resources