How can I save an interactive Matplotlib figure to excel or just as figure? I just want to share those with non-technical users. However, I can do that using Plotly and share the link but because of data privacy issues, they don't prefer that method. Is there any method I can do that?
I have interactive images like below. Some are stacked together. So need to zoom that's why I need to save images in an interactive mood. Then non-technical users can zoom in and check the results.
import pandas as pd
import matplotlib
import mplcursors
data = [['Alex',10000,15000,6705],['Bob',12000,11050,7050],['Clarke',10300,10450,9050],['Alan',10100,15500,6205],['Sam',10300,15050,6785]]
df = pd.DataFrame(data,columns=['Name','Year2019','Year2020','Year2021'])
dfLen = len(df)
x =df['Year2020']
if dfLen > 0:
%matplotlib notebook
%matplotlib notebook
import matplotlib.pyplot as plt
fig , ax = plt.subplots(figsize=(8,6))
a = df['Year2019']/100
# Create a scatter plot (Use s - make the size of each vendors usage)
plt.scatter('Year2019', 'Year2020',s= x , c='Year2019', data=df)
plt.title("2019 Vs 2020 sold items",fontsize=12)
plt.xlabel('2019', fontsize=10)
plt.ylabel('2020',fontsize=10)
list1 = list(df['Name'])
i = 0;
for row in df.itertuples():
h = list1[i]
i=i+1
h = str(h)
c = row.Year2019
d = row.Year2020
ax.text(c,d,s = h, size = 8)
crs = mplcursors.cursor(ax,hover=True)
crs.connect("add", lambda sel: sel.annotation.set_text(
'Point x - {} ,\n'
'y- {}'.format(sel.target[0], sel.target[1])))
plt.show()
Related
I'm fairly new to Python and I'm struggling annotating plots at the minute.
I've come from R so I'm used to the ease of being able to annotate scatterplot points with minimum code.
Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
url = ('https://fbref.com/en/share/nXtrf')
df = pd.read_html(url)[0]
df = df[['Unnamed: 1_level_0', 'Unnamed: 2_level_0', 'Play', 'Perf']].copy()
df.columns = df.columns.droplevel()
df = df[['Player','Squad','Min','SoTA','Saves']]
df = df.drop([25])
df['Min'] = pd.to_numeric(df['Min'])
df['SoTA'] = pd.to_numeric(df['SoTA'])
df['Saves'] = pd.to_numeric(df['Saves'])
df['Min'] = df[df['Min'] > 1600]['Min']
df = df.dropna()
df.plot(x = 'Saves', y = 'SoTA', kind = "scatter")
I've tried numerous ways to annotate this plot. I'd like the points to be annotated with corresponding data from 'Player' column.
I've tried using a label_point function that I've found while trying to find a work around buy I keep getting Key Error 0 on most ways I try.
Any assistance would be great. Thanks.
You could loop through both columns and add a text for each entry. Note that you need to save the ax returned by df.plot(...).
ax = df.plot(x='Saves', y='SoTA', kind="scatter")
for x, y, player in zip(df['Saves'], df['SoTA'], df['Player']):
ax.text(x, y, f'{player}', ha='left', va='bottom')
xmin, xmax = ax.get_xlim()
ax.set_xlim(xmin, xmax + 0.15 * (xmax - xmin)) # some more margin to fit the texts
An alternative is to use the mplcursors library to show an annotation while hovering (or after a click):
import mplcursors
mplcursors.cursor(hover=True)
I am quite new to python so please bear with me.
My code is below:
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
df = pd.read_csv(r"/Users/aaronhuang/Desktop/ffp/exfileCLEAN2.csv", skiprows=[1])
magnitudes = df['Magnitude '].values
times = df['Time '].values
zscores = np.abs(stats.zscore(magnitudes, ddof=1))
outlier_indicies = np.argwhere(zscores > 3).flatten()
print(times[outlier_indicies])
window = 10
num = 1
x = times[outlier_indicies[num]-window:outlier_indicies[num]+window+1]
y = magnitudes[outlier_indicies[num]-window:outlier_indicies[num]+window+1]
plt.plot(x, y)
plt.xlabel('Time (units)')
plt.ylabel('Magnitude (units)')
plt.show()
fig = plt.figure()
fig.savefig("/Users/aaronhuang/Downloads")
Is there a way I can print all the graphs separately once?. Deleting num has not worked.
Thank you in advance.
You can put the plots inside a for loop, and repeat as many times as you like, with different variables for each step if needed. Most software will show the plots either in multiple plot windows, or output them in a long strip you can scroll through. If you use Spyder however, they will play back to back, with each plot being a frame.
Also, if you want to print multiple plots, you have to put the plt.show() inside the loop as well, putting it after the loop will show all the values on a single plot.
For example:
import matplotlib.pyplot as plt
x_values = [1,2,3,4,5,6,7]
for x in x_values:
y = x**2
plt.plot(x,y,"o")
plt.axis([0,50, 0,50])
plt.show()
Hello I am attempting to write a program that allows the plotting of the graph from various data sets from a excel database.(The x axis is a fixed set of values while the data values from other columns can be selected). However, the graph that is plotted only contains the axes of the graph, while the data points are completely missing. The code I have used is as such:
import xlrd
import matplotlib.pyplot as plt
from matplotlib.figure import *
loc = ("C:\\Users\\yeoho\\DCO_Raw_Data.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0,0)
x = [[sheet.cell_value(r,0)]for r in range(6,sheet.nrows)]
checkOn = True
while checkOn:
FileName = [[sheet.cell_value(0,c)]for c in range(1,13)]
print(FileName)
print("Enter the Integer (1-n) corresponding to the file name that you would like to plot")
z = int(input())
y = [[sheet.cell_value(r,z)]for r in range(6,sheet.nrows)]
fig = plt.figure()
ax = fig.add_subplot(111)
assert len(x) == len(y)
for i in range(len(x)):
plt.plot(x[i],y[i],color='black')
plt.show()
break
The code in lines 16-21 were taken from another stackoverflow page. How to plot two lists of tuples with Matplotlib
The original code did not have a color parameter but I have found out that that is not the source of the issue.
I am unsure of what the issue here is. Thank you for taking your time to read this and I hope you can help me with this issue.
I'm trying to generate some diagrams from an .h5 file but I don't know how to do it.
I'm using pytables, numpy and matplotlib.
The hdf5 files I use contains 2 sets of data, 2 differents curves.
My goal is to get diagrams like this one.
This is what I managed to do for the moment:
import tables as tb
import numpy as np
import matplotlib.pyplot as plt
h5file = tb.openFile(args['FILE'], "a")
for group in h5file.walkGroups("/"):
for array in h5file.walkNodes("/","Array"):
if(isinstance(array.atom.dflt, int)):
tab = np.array(array.read())
x = tab[0]
y = tab[1]
plt.plot(x, y)
plt.show()
x and y values are good but I don't know how to use them, so the result is wrong. I get a triangle instead of what I want ^^
Thank you for your help
EDIT
I solved my problem.
Here is the code :
fig = plt.figure()
tableau = np.array(array.read())
x = tableau[0]
y = tableau[1]
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(x)
ax2.plot(y)
plt.title(array.name)
plt.show()
How do I plot a barchart similar to
Clustered bar plot in gnuplot using python matplotlib?
date|name|empid|app|subapp|hours
20140101|A|0001|IIC|I1|2.5
20140101|A|0001|IIC|I2|3
20140101|A|0001|IIC|I3|4
20140101|A|0001|CAR|C1|2.5
20140101|A|0001|CAR|C2|3
20140101|A|0001|CAR|C3|2
20140101|A|0001|CAR|C4|2
Trying to plot the subapp hours by app for the same person. Couldn't see an example in the demo pages of matplotlib.
EDIT: None of the examples cited below seem to work for unequal # of bars for each category as above.
The examples didn't manage unequal # of bars but you can use another approach. I'll post you an example.
Note: I use pandas to manipulate your data, if you don't know about it you should give it a try http://pandas.pydata.org/:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
df = pd.read_table("data.csv",sep="|")
grouped = df.groupby('app')['hours']
colors = "rgbcmyk"
fig, ax = plt.subplots()
initial_gap = 0.1
start = initial_gap
width = 1.0
gap = 0.05
for app,group in grouped:
size = group.shape[0]
ind = np.linspace(start,start + width, size+1)[:-1]
w = (ind[1]-ind[0])
start = start + width + gap
plt.bar(ind,group,w,color=list(colors[:size]))
tick_loc = (np.arange(len(grouped)) * (width+gap)) + initial_gap + width/2
ax.set_xticklabels([app for app,_ in grouped])
ax.xaxis.set_major_locator(mtick.FixedLocator(tick_loc))
plt.show()
And on data.csv is the data:
date|name|empid|app|subapp|hours
20140101|A|0001|IIC|I1|2.5
20140101|A|0001|IIC|I2|3
20140101|A|0001|IIC|I3|4
20140101|A|0001|CAR|C1|2.5
20140101|A|0001|CAR|C2|3
20140101|A|0001|CAR|C3|2
20140101|A|0001|CAR|C4|2