I have a Timeseries data for leaks which is a csv file and I have another excel file which have date when the leak has occured. I want to plot this on same graph for visulaization and to point when leak has occured. I am not able to plot this on same graph using matplotlib. Can someone help me with this. I am using python programming.
import matplotlib.pyplot as plt
rows = 3
cols = 4
fig, axes = plt.subplots(figsize=(30,10))
df.plot(ax=axes, rot=60)
I was trying to use the above matplot lib code to plot.
This one is the Timeseries data which i have from the database for sensors.
This image has date on which leak has occured.
Related
I am using Tensorboard 2 to visualize my training data and I am able to save scalar plots to disk. However, I am unable to find a way to do this for histogram plots (tf.summary.histogram).
Is it possible to save histogram plots from Tensorboard 2 to disk, just like it is possible to do with scalars? I have looked through the documentation and it seems like this is not supported, but I wanted to confirm with the community before giving up. Any help or suggestions would be greatly appreciated.
There is an open issue to add a download button for histograms. However, this issue is open for more than 4 years, so I doubt it is getting resolved soon.
A workaround is to use the url that tensorboard would use to get the data.
A short example:
# writing some data to tensorboard
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter('./tmp')
writer.add_histogram('hist', np.arange(10), 0)
Open tensorboard in the browser (here localhost:6006):
Get data as JSON using the template
http://<tb-host>/data/plugin/histograms/histograms?run=<run-name>&tag=<tag-name>.
Here http://localhost:6006/data/plugin/histograms/histograms/?run=.&tag=hist:
Now you can download the data as JSON.
Quick comparison with matplotlib:
import pandas as pd
import json
import matplotlib.pyplot as plt
with open('histograms.json', 'r') as f:
d = pd.DataFrame(json.load(f)[0][2])
fix, axes = plt.subplots(1, 2, figsize=(10, 3))
axes[0].bar(d[1], d[2])
axes[0].set_title('tb')
axes[1].hist(data)
axes[1].set_title('original')
I have a simple .csv that I'd like to render PNG files of pie charts from, which from other StackOverflow questions I've gotten mostly working.
The one change I can't figure out is how to show both values and percentages on the pie slices. There's a similar answer here, but not for a CSV dataframe and I can't grok how to adapt this to fit my dataframe.
My interactive session commands to generate the pie chart follow:
import pandas as pd
from matplotlib.pyplot import pie, axis, show
df = pd.read_csv('syntheticdata.csv')
# show the csv: print(df.head())
sums = df.groupby(df["category"])["capacity-tb"].sum()
axis('equal');
pie(sums, labels=sums.index, autopct='%.0f%%');
show()
My pie chart is almost how I want it aside from missing the actual numbers behind the percentages.
pie chart example
My synthetic test data csv is available below:
svm,capacity-tb,available-tb,category
cifs-share,54,28,Fileshare
foo-cifs,19,4,Project Data
VM02,109,49,Virtual Machine Storage
nfs01,40,22,Project Data
VM-02,65,31,Virtual Machine Storage
VM-01,65,44,Virtual Machine Storage
nfs-foo-01,43,29,Project Data
cifs-project-01,20,1,Project Data
nfs-foo-01_dest_0,21,3,Replication & Disaster Recovery
cifs-project-01_dest_0,19,1,Replication & Disaster Recovery
nfs01-dr,27,7,Replication & Disaster Recovery
cifs-share-dr,60,20,Replication & Disaster Recovery
VM02-dr,123,52,Replication & Disaster Recovery
foo-cifs-dr,19,4,Replication & Disaster Recovery
Modify the absolute_value function:
import matplotlib.pyplot as plt
import numpy as np
sums = df.groupby(df["category"])["capacity-tb"].sum()
pie, ax = plt.subplots(figsize=[10, 6])
def absolute_value(val):
return f'{np.round(val/100.*sums.values.sum(), 0)} {round(val,1)}%'
plt.pie(x=sums, autopct=absolute_value, explode=[
0.05]*4, labels=sums.index, pctdistance=0.5)
OUTPUT:
I intended to plot a trend of daily BTC prices, which is recorded as a csv file, consisting of a sequence of 3892 numbers.
Being a csv file, I could use microsoft excel to plot the trend.
It looks like this;
Meanwhile, I attempted to do the same thing using pandas and matplotlib in a jupyter notebook, which doesn't look good;
What's wrong with my implementation?
One more thing is the implementing time ; it takes a quite a while, like one or two minute.
This is not the case when I put a code and implement like this ;
plt.plot(list(range(1000)),list(range(1000)))
Here is the full code for my attempt.
import pandas as pd
from matplotlib import pyplot as plt
df=pd.read_csv('BTC_inv_daily.csv')
Close=df['Close'].tolist()
N=len(Close)
plt.plot(list(range(N)),Close)
plt.show()
I am using python 3.8 on Windows 10; trying to make a plot with about 700M points in it, sound wave analysis. Here: Interactive large plot with ~20 million sample points and gigabytes of data
Vaex was highly recommended. I am trying to use examples from the Vaex tutorial but the graph does not appear. I could not find a good example on Internet.
import vaex
import numpy as np
df = vaex.example()
df.plot1d(df.x, limits='99.7%');
The Vaex documents don't mention that pyplot.show() should be used to display. Plot1d plots a histogram. How to plot just connected points?
I am pretty sure that the vaex documentation explains that the (now deprecated) method .plot1d(...) is a wrapper around matplotlib plotting routines.
If you would like to create custom plots using the binned data, you can take this approach (I also found it in their docs)
import vaex
import numpy as np
import pylab as plt
# Load example data
df = vaex.example()
# Do the binning yourself
counts = df.count(binby=df.x, shape=64, limits='99.7%')
# Take care of the x-axis
limits = df.limits_percentage(df.x, percentage=99.7)
xvals = np.linspace(limits[0], limits[1], num=64)
# Create your custom plot via matplotlib, plotly or your favorite tool
p.plot(xvals, counts, marker='o', ms=5);
Link to the Spreadsheet: https://docs.google.com/spreadsheets/d/1c2hItirdrnvz2emJ4peJHaWrQlzahoHeVqetgHHAXvI/edit?usp=sharing
I am new to Python and am very keen to learn this since I like statistics and computer programming. Any help would be appreciated!
I used matploylib and numpy, but don't know how to graph this spreadsheet as a line graph.
If the data are in a common csv (comma separable values) format, they can easily be read into python. (Here I downloaded the file from the link in the question via File/Download as/comma separated values.
Using pandas and matplotlib
You can then read in data in pandas using pandas.read_csv(). This creates a DataFrame. Usually pandas automatically understands that the first row is the column names. You can then access the columns from the Dataframe via their names.
Plotting can easily performed with the DataFrame.plot(x,y) method, where x and y can be simply the column names to plot.
import pandas as pd
import matplotlib.pyplot as plt
# reading in the dataframe from the question text
df = pd.read_csv("data/1880-2016 Temperature Data Celc.csv")
# make Date a true Datetime
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
# plot dataframe
ax = df.plot("Year", "Temperature in C")
ax.figure.autofmt_xdate()
plt.show()
In case one wants a scatterplot, use
df.plot( x="Year", y="Temperature in C", marker="o", linestyle="")
Using numpy and matplotlib
The same can be done with numpy. Reading in the data works with numpy.loadtxt where one has to provide a little bit more information about the data. E.g. expluding the first row and using comma as separator. The unpacked columns can be plotted with pyplot pyplot.plot(year, temp).
import numpy as np
import matplotlib.pyplot as plt
# reading in the data
year, temp = np.loadtxt("data/1880-2016 Temperature Data Celc.csv",
skiprows=1, unpack=True, delimiter=",")
#plotting with pyplot
plt.plot(year, temp, label="Temperature in C")
plt.xlabel("Year")
plt.ylabel("Temperature in C")
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
The result looks roughly the same as in the pandas case (because pandas simply uses matplotlib internally).
In case one wants a scatterplot, there are two options:
plt.plot(year, temp, marker="o", ls="", label="Temperature in C")
or
plt.scatter(year, temp, label="Temperature in C")