Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1D plot frome 3D data
#1
Hi all, I use python and I want to draw a 1D curve from 3D data i.e. I have data in the form of x, y, z and in the fourth column a variable D Also, I want that before drawing the curve to have the data of the curve in a .txt file? Please, can someone help me, I need a method that will allow me to do this task? Thanks in advance
Reply
#2
what is 1D curve? Probably you mean 2D
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yes, sorry, I mean 2D plot
Reply
#4
So what data are you starting with?

Where are you getting the data to plot the 3D curve?

How are y and z related to x?
Reply
#5
Hello,
Here is the data I want plotted also I want a 2D plot and not 3d I for example x as function of D.
.csv   DATA.csv (Size: 2.45 KB / Downloads: 1)
Reply
#6
Well X an Y are all zero, so I can only think you want to plot z against D!

Probably, you can get the numpy array coords below directly using numpy, but I never use numpy, so I don't know how to do that. Also I hardly ever use mathplotlib, so I am no expert.

Try out the following in your Python shell, step for step:

import csv
import glob
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)

path = '/home/pedro/myPython/csv/csv/'
csvs = glob.glob(path + '*.csv')
for file in csvs:
    print('The csv files are', file)
myfile = input('Copy and paste the file you want here ... ')
# csv_reader is gone after you use it one time
# it is convenient to copy the data to a list
with open(myfile) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    data = []
    for row in csv_reader:
        data.append(row)
Zs = []
Ds = []
for d in range(1, len(data)):
    print(data[d][2], data[d][3])
    Zs.append(int(data[d][2]))
    Ds.append(float(data[d][3]))
# show the data in the lists           
for i in range(len(Zs)):
    print(Zs[i], Ds[i])

minimumZ = min(Zs)
maximumZ = max(Zs)
minimumD = min(Ds)
maximumD = max(Ds)
print('The minimum and maximum Z are', minimumZ, maximumZ)
print('The minimum and maximum D are', minimumD, maximumD)

# no difference if you use () or [] so only use 1 of these
coords = np.array([Zs, Ds])
coords = np.array((Zs, Ds))
for c in coords:
    print(c)

def tp1():
    plt.rcParams["figure.figsize"] = [10, 10]
    maximumD = max(Ds)
    x, y = coords
    fig, ax = plt.subplots(num="Showing y = D ")  # Create a figure and an axes.
    ax.plot(x, y, label='y = D')  # Plot some data on the axes.    
    ax.set_xlabel('x values')  # Add an x-label to the axes.
    ax.set_ylabel('y values')  # Add a y-label to the axes.
    ax.set_title("As z gets bigger, D gets bigger")  # Add a title to the axes.
    plt.axhline(y = maximumD, color = 'r', linestyle = 'dashed', label='y = D')
    ax.legend()  # Add a legend.
    fig.text(.4, .25, "The ups and downs", va="center", ha="center",
             bbox=dict(boxstyle="round, pad=1", facecolor="w"))
    plt.show()

# now just run the function tp1()
tp1()
mathplotlib can do many things as decoration. Look up the docs!
Reply
#7
Thank you very much!! , but I see that the value of Z repeats and I want to gather the values of D for each value of Z So I can have the curve that I want
Reply
#8
I missed out the lines above which select the csv file and open it. Have another look now, lines 9, 10, 11.

No, there are no repeated Z values:

Quote:len(Zs)
80
setZ = set(Zs)
len(setZ)
80

The length of the set(Zs) is the same as the length of Zs. If there were repeated Z values, len(setZ) would be smaller.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract and plot data from a txt file usercat123 2 1,253 Apr-20-2022, 06:50 PM
Last Post: usercat123
  Searching Module to plot large data G_rizzle 0 1,473 Dec-06-2021, 08:00 AM
Last Post: G_rizzle
  Not able to figure out how to create bar plot on aggregate data - Python darpInd 1 2,328 Mar-30-2020, 11:37 AM
Last Post: jefsummers
  HeatMap plot with arduino serial data tshivam 0 3,236 Oct-08-2018, 10:57 AM
Last Post: tshivam
  How to plot data to the same figure for every run? chry5ler 3 4,849 Aug-03-2018, 04:21 PM
Last Post: heras

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020