I can't seem to figure out how to plot a graph as i always get an error message of "no numeric data to plot". I have also tried using a csv file to plot but it has not been successful.
This is my 2d list;
listofstock.append([1,"Microsoft","Mega",100,188,207])
listofstock.append([2,"Amazon","Mega",5,1700,3003])
listofstock.append([3,"PayPal","Large",80,100,188])
listofstock.append([4,"Apple","Large",100,60,110])
listofstock.append([5,"Fastly","Mid",30,40,76])
listofstock.append([6,"Square","Mid",30,40,178])
You can try this
import pandas as pd
listofstock = []
listofstock.append([1,"Microsoft","Mega",100,188,207])
listofstock.append([2,"Amazon","Mega",5,1700,3003])
listofstock.append([3,"PayPal","Large",80,100,188])
listofstock.append([4,"Apple","Large",100,60,110])
listofstock.append([5,"Fastly","Mid",30,40,76])
listofstock.append([6,"Square","Mid",30,40,178])
# if you are in a ipython-notebook
pd.DataFrame.from_records(listofstock).drop(0, axis=1).set_index([1]).plot()
# if you want to save the figure to a file
fig = pd.DataFrame.from_records(listofstock).drop(0, axis=1).set_index([1]).plot().get_figure()
fig.savefig('test.png')
# if you want to open in a new window
fig.show()
Related
I am trying to save scatter plots from a list of dataframes in one pdf file using this:
pdf = matplotlib.backends.backend_pdf.PdfPages("./output.pdf")
for df in list_degs_dfs:
dfplt = df.plot.scatter("DEratio", "MI_scaled")
pdf.savefig(dfplt)
pdf.close()
But I get this error:
ValueError: No figure AxesSubplot(0.125,0.11;0.775x0.77)
I thought it can be an issue with converting the plots to matplotlib figure and tried this:
import matplotlib.pyplot as plt
pdf = matplotlib.backends.backend_pdf.PdfPages("./output.pdf")
for df in list_degs_dfs:
dfplt = df.plot.scatter("DEratio", "MI_scaled")
dfplt = plt.figure(dfplt)
pdf.savefig(dfplt.Figure)
pdf.close()
and got this:
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'AxesSubplot'
How to save all dfplt figrues from all df dataframes from a list of dataframes in one file?
savefig should not take any arguments, it will save the currently active figure handle.
pdf = matplotlib.backends.backend_pdf.PdfPages("./output.pdf")
for df in list_degs_dfs:
dfplt = df.plot.scatter("DEratio", "MI_scaled")
pdf.savefig() # saves the active handle
pdf.close()
Context: I've two data frames that i read with pandas from .csv files, one of them (dfevents) has a latitude and longitude fields on it, the other dataframe (dfplacedetails) has multiple points that creates a polygon. I'm usign "intersets" properties to verify when the first data frame cross with the polygon of the other one. That actually works fine, but when I'm triying to plot both of the layers is just not posibble, they plot separete
My code is as follow:
# Libraries
from matplotlib import pyplot as plt
import geopandas as gp
import pandas as pd
# Creating data frames
dfevents = pd.read_csv (r'C:\Users\alan_\Desktop\TAT\Inputs\Get Events\Get_Events.csv')
print(dfevents)
dfplacedetails = pd.read_csv (r'C:\Users\alan_\Desktop\TAT\Inputs\Get Place Details\Get_Place_Details.csv')
print(dfplacedetails)
# Make them proper Geometrys
dfevents['point'] = gp.GeoSeries.from_xy(dfevents.longitude, dfevents.latitude)
dfplacedetails['polygon'] = gp.GeoSeries.from_wkt('POLYGON' + dfplacedetails.polygon)
# Make them GeoDataFrames
dfevents = gp.GeoDataFrame(dfevents, geometry='point')
dfplacedetails = gp.GeoDataFrame(dfplacedetails, geometry='polygon')
# Output (It works fine)
dfout = dfevents.intersects(dfplacedetails)
print(dfout)
# Plot
fig, ax =plt.subplots(figsize =(20,10))
dfplacedetails.plot(ax=ax, color='blue')
dfevents.plot(ax=ax, color='red',markersize=10)
ax.set_axis_on()
The result that i got when I plot as I described up in my code is as follow:
But when I plot separate both of the layers plot fine:
Is there any way to plot both of them in the same image?
Thanks for you help!
By the way i'm using Visual Studio Code
During debugging or computationally heavy loops, i would like to see how my data processing evolves (for example in a line plot or an image).
In matplotlib the code can redraw / update the figure with plt.cla() and then plt.draw() or plt.pause(0.001), so that i can follow the progress of my computation in real time or while debugging. How do I do that in plotly express (or plotly)?
So i think i essentially figured it out. The trick is to not use go.Figure() to create a figure, but go.FigureWidget() Which is optically the same thing, but behind the scenes it's not.
documentation
youtube video demonstration
Those FigureWidgets are exactly there to be updated as new data comes in. They stay dynamic, and later calls can modify them.
A FigureWidget can be made from a Figure:
figure = go.Figure(data=data, layout=layout)
f2 = go.FigureWidget(figure)
f2 #display the figure
This is practical, because it makes it possible to use the simplified plotly express interface to create a Figure and then use this to construct a FigureWidget out of it. Unfortunately plotly express does not seem to have it's own simplified FigureWidget module. So one needs to use the more complicated go.FigureWidget.
I'm not sure if an idential functionality exists for plotly. But you can at least build a figure, expand your data source, and then just replace the data of the figure without touching any other of the figure elements like this:
for i, col in enumerate(fig.data):
fig.data[i]['y'] = df[df.columns[i]]
fig.data[i]['x'] = df.index
It should not matter if your figure is a result of using plotly.express or go.Figure since both approaches will produce a figure structure that can be edited by the code snippet above. You can test this for yourself by setting the two following snippets up in two different cells in JupyterLab.
Code for cell 1
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
# code and plot setup
# settings
pd.options.plotting.backend = "plotly"
# sample dataframe of a wide format
np.random.seed(5); cols = list('abc')
X = np.random.randn(50,len(cols))
df=pd.DataFrame(X, columns=cols)
df.iloc[0]=0;df=df.cumsum()
# plotly figure
fig = df.plot(template = 'plotly_dark')
fig.show()
Code for cell 2
# create or retrieve new data
Y = np.random.randn(1,len(cols))
# organize new data in a df
df2 = pd.DataFrame(Y, columns = cols)
# add last row to df to new values
# this step can be skipped if your real world
# data is not a cumulative process like
# in this example
df2.iloc[-1] = df2.iloc[-1] + df.iloc[-1]
# append new data to existing df
df = df.append(df2, ignore_index=True)#.reset_index()
# replace old data in fig with new data
for i, col in enumerate(fig.data):
fig.data[i]['y'] = df[df.columns[i]]
fig.data[i]['x'] = df.index
fig.show()
Running the first cell will put together some data and build a figure like this:
Running the second cell will produce a new dataframe with only one row, append it to your original dataframe, replace the data in your existing figure, and show the figure again. You can run the second cell as many times as you like to redraw your figure with an expanding dataset. After 50 runs, your figure will look like this:
I have an excel file with my data in sheet named 'main'.
I want to plot a line plot (or scatter) for particular cells in the 'main' sheet
The data I want to use in 'main' is:
X-axis data is in column A i.e. from A36 to A136
and
Y-axis data is in column A i.e. from G36 to G136
Here is the code I used to make the simpler version of the plot
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import pandas as pd
x = pd.read_excel('ob_half_cd100_titration.xlsx', 'test', parse_cols='A')
y = pd.read_excel('ob_half_cd100_titration.xlsx', 'test', parse_cols='B')
plt.plot(x, y)
plt.show()
The final figure should look like the following image (made from the 'test' sheet):
Link to the excel file :
https://www.dropbox.com/s/2pq4pzq7y7ng29e/ob_half_cd100_titration.xlsx?dl=0
Use a slice of the data:
plt.plot(x[35:136], y[35:136])
I have the following code:
import pandas as pd
import matplotlib
matplotlib.style.use('ggplot')
df = pd.DataFrame({ 'sample1':['foo','bar','bar','qux'], 'score':[5,9,1,7]})
sum_df = df.groupby("sample1").sum()
pie = sum_df.plot(kind="pie", figsize=(6,6), legend = False, use_index=False, subplots=True, colormap="Pastel1")
Which makes the pie chart. What I want to do then is to save it to a file.
But why this fail?
fig = pie.get_figure()
fig.savefig("~/Desktop/myplot.pdf")
I get this error:
'numpy.ndarray' object has no attribute 'get_figure'
Well pie is a numpy array because the return type for DataFrame.plot() is a numpy array of matplotlib.AxesSubplot objects.
fig = pie[0].get_figure()
fig.savefig("~/Desktop/myplot.pdf")
Claim: My solution is save the current plot which works here, but it's not a good way to do this. What #user3100115 posted is the right way to do this.
Using matplotlib.pyplot.savefig to save it:
import matplotlib.pyplot as plt
plt.savefig('pie')
You'll get a image named pie.png like this: