Adding a line to a Pandas plot - python-3.x

I'm currently tracking my internet speed and want to generate a plot of my measurements with a Timestamp, Upload value and Download value.
I'm using this to create the plot
df.plot(
kind='line',
x=timestamp_column_name,
y=[download_column_name, upload_column_name],
figsize=(12,5)
)
Generated plot:
Now I would like to add a line to this plot with the constant height of y=100000 but I can't figure out how to do this correctly. How should I do this with Pandas?

You can use axhline. Since df.plot() is a wrapper for matplotlib and returns the Matplotlib axes, which contain all the methods for interacting with the plot, it can be used straight forward as:
ax = df.plot( ... )
ax.axhline(y=100000)

Related

How to Only plot Confidence Interval in Seaborn Line Plot

Is there a way to only plot the confidence interval using seaborn lineplot (or another seaborn method)? For example, I used seaborn lineplot to produce this graph:
To produce this graph I just have something like:
sns.lineplot(data=df, ax=x, x='day_of_week', y='y_variable', color='lightgrey'
Since I have a lot of data points per "day of week" which is "Monday", "Tuesday", etc. I just wanted to use the default 95% CI. Is there a way to just plot the CI without the darker line in the middle (which I assume is the mean)?
You can use linewidth=0:
tips = sns.load_dataset('tips')
sns.lineplot(data=tips, x='day', y='total_bill', linewidth=0)
Output:

Creating a surface plot from an Unstructured grid vtk file using Vedo

I have an unstructured grid vtk file that contains three different types of cells (Tetrahedral, Wedge and Hexahedral). This file contains multiple Scalars (8 attributes such as Pressure, Temperature e.t.c.) and a Single Vector (U,V,W) and I am trying to create a surface plot from this file for a Scalar or Vector at a time using the Vedo python wrapper for vtk. The vtk file contains a scalar or vector value for each cell, including the point coordinates.
I have read the documentation over and over, with examples here https://vtkplotter.embl.es/content/vtkplotter/index.html. These are the things that I have tried with the challenge that I am having with each method:
Method 1: Loading the file as a TetMesh
vp = Plotter()
test = load('Case_60.vtk')
vp.show(test)
This method doesn't plot Scalar Values and only shows points. No Solid Surface. Tried using a cuttertool() with it , it throws an error saying non-Tetrahedral Cell Encountered.
Method 2: Using the UGrid
ug = UGrid('Case_60.vtk')
show(ug)
This method plots as surface with a solid color. Does not seem to be picking the Scalars.
What is the proper way for me to display surface plot and display the scalar value for each cell? Is Vedo able to do what I'm trying to do?
You might need to specify which array is to be used for coloring, e.g.:
from vedo import *
ug = UGrid(datadir+'limb_ugrid.vtk')
print(ug.getArrayNames())
ug.selectCellArray('chem_0')
show(ug, axes=True)
if this doesn't work for your mesh please submit an issue here.

Python - Pdf file with several figures in one page (not with subplots !!)

I'm trying to produce a pdf file (with PdfFile) containing several figures in one page.
The most obvious solution is to use subplots. However, in my case this is not possible because each plot is produced by a different function. For example, there is a function def plotPDF(inputData) that will plot a probability distribution function (PDF), and another function def plotCDF(inputData) that will plot a cumulative distribution function (CDF). My code contains up to 20 different functions that will produce different plots when they are called.
What I want to do is to select some of these plots and produce a pdf file where they are contained in the same page. Following the example of PDF and CDF, I would like to produce a pdf file which contains one page where both plots are next to each other (in a similar way to the subplots).
I have tried to do this with subplots, but I cannot directly call the function within a subplot. That is, the following code wouldn't work:
fig, ax = plt.subplots(nrows=1, ncols=2)
plt.subplot(1, 2, 1)
plotPDF(inputData)
plt.subplot(1, 2, 2)
plotCDF(inputData)
plt.show()
Does anybody know how to solve this issue ? I need to proceed like this because I need the plot functions to be independent for other purposes. Making subplots would mean changing this structure, and it would make the code less versatile.
Thanks in advance !
I don't know if there's a way to do what you are asking, maybe someone else will know...
but
the recommended way to write a plotting function is to pass a reference to an Axes object to the function, and write the function to use that axes to do the plotting.
so in your case:
def plotPDF(data, ax=None):
ax = ax or plt.gca() # if no axes, use current axes
plt.sca(ax) # set axes as current axes (important if you are using the pyplot API)
# the rest of the function goes here
def plotCDF(data, ax=None):
ax = ax or plt.gca()
plt.sca(ax)
(...)
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
plotPDF(inputData, ax=ax1)
plotCDF(inputData, ax=ax2)
plt.show()
Have you read this answer to a similar post? The answers in that post contain many ways to save an image into a pdf file, and only one (the matplotlib backend) requires subplots, as far as I know.
You can also save the files separately as png files, then use LaTeX / Word / another primitive way to arrange them into a pdf, which could be tedious.
Otherwise, could you maybe elaborate why using subplots wouldn't work with your functions? Maybe there is a way to use subplots, but then you'll need to show us the code.

Main figure legend outside of subplots

I have a number of subplots within a single figure. Each figure plots multiple lines that represent the same thing (represented by color) but in different situations (different subplots). I would like to create a legend at the base of the figure showing what the color of the line means. However, I running into a problem with getting the legend to not overlap the subplots and if I can adjust the axes, getting the legend to save.
I have tried a few different solutions with some help here but have been unable to adapt to subplots. Below is an example code that I am working with.
import numpy as np
import matplotlib.pyplot as plt
m1=1
m2=10
x=np.linspace(0,100,num=101,endpoint=True)
y1m1=m1*x**2
y2m1=m1*x**0.5
y1m2=m2*x**2
y2m2=m2*x**0.5
fig=plt.figure(figsize=(4,4))
ax1=fig.add_subplot(211)
ax1.plot(x,y1m1,'b',label=r'$x^2$')
ax1.plot(x,y2m1,'r',label=r'$\sqrt{x}$')
ax2=fig.add_subplot(212)
ax2.plot(x,y1m2,'b')
ax2.plot(x,y2m2,'r')
fig.legend(loc='lower center',ncol=2)
fig.tight_layout()
fig.savefig('examplefig.png',dpi=300)
plt.show()
My goal is to save the output to a png for a good figure.
This is one way of doing it using the suggestion provided here. The idea is to add the legend at position with respect to a given axis object. In your case, since you want to add the legend at the base, it is preferred you specify the position relative to ax2. Using ncol=2 is a matter of personal choice.
fig=plt.figure(figsize=(4,4))
ax1=fig.add_subplot(211)
l1, = ax1.plot(x,y1m1,'b')
l2, = ax1.plot(x,y2m1,'r')
ax2=fig.add_subplot(212)
ax2.plot(x,y1m2, 'b')
ax2.plot(x,y2m2, 'r')
ax2.legend(handles = [l1,l2] , labels=[r'$x^2$', r'$\sqrt{x}$'],
bbox_to_anchor=(0.7, -0.2), ncol=2)
fig.tight_layout()

Matplotlib legend was not displayed properly

I am trying to plot the same geospatial data reading this tutorial:
https://www.datacamp.com/community/tutorials/geospatial-data-python
But the legend of her final graph has shwown wind speed in ranges. But when I used the same code mentioned on her tutorial, I could not produce the same legend. Any thing missing in her code? or What is wrong?
The categorical legend entries are provided via PySAL, and seem to need the scheme=<> and legend=True arguments. Perhaps these were set by default in previous versions of the libraries. For me, the following works ok:
fig, ax = plt.subplots(1, figsize=(20, 20))
base = country[country['NAME'].isin(['Alaska','Hawaii']) == False].plot(
ax=ax, color='#3B3C6E')
florence.plot(
ax=ax, column='Wind', marker="<", markersize=10,
cmap='cool', scheme="Quantiles", legend=True)
plt.axis('off')
Unfortunately the markers do not seem to be inherited into the legend, but the colors are the more salient difference anyway.
The geopandas.plot docs indicate 3 supported schemes: Quantiles, Equal_interval, fisher_jenks -- the first one seems to correspond to the datacamp example chart. See also the parameter k which defines the number of classes (5 is default as this example).

Resources