How to set Interval Unit of chart label using openpyxl - python

I am trying to set Interval Unit (for interval between labels) of chart using openpyxl. This option is set to 'Automatic' by default.
Image shows how we can set the option manually in Excel. Image Link
I found this option in XlsxWriter:
chart.set_x_axis({'interval_unit': 5})
but could not find the option in openpyxl.
Please help.

For bar and column charts in openpyxl the axis labeling individual data is of type openpyxl.chart.axis.TextAxis. TextAxis features a property named tickLblSkip which defines what you are looking for. You can set a labeling interval of 5 as follows:
from openpyxl.chart import BarChart
chart = BarChart
chart.x_axis.tickLblSkip = 5

Related

Is there a way to stagger or dodge multiple matplotlib.plot error bars?

I am trying to view the std error bar for each dataset but they are overlapping each other. Is there a way to stagger the error bar for each dataset?
Here is the code I am using:
group=hms.groupby([ hms.index.month]).mean()
std=hms.groupby([ hms.index.month]).std()
group.plot( linewidth=2,yerr=std)
[enter image description here][1]
Line Graph with Error bars
Seaborn's pointplot has that option baked in. Just set the parameter dodge to True.
You'll probably have to reformat your data into a "long" format though. Then create a new column with just the months to use as your x axis. I can't tell you exactly how without sample data.
sns.pointplot(x='month', y='value', hue='group', data=hms, ci='std', dodge=True)
Otherwise, you can just add shift your x values by some small amount for each group and use the standard matplotlib library.

Python Pandas Bar plot shows no color

When I follow the examples in the pandas documentation for visualizing a bar chart:
https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.plot.bar.html
See the first example titled Basic plot. with 3 bars.
The pandas documentation shows:
But when I type the same in my local jupyter notebook, I get no color:
Why does my notebook not have color? What can I do to display the colors?
You can pass custom colors to df.plot:
df = pd.DataFrame({"lab":["A","B","C"], "val":[10,20,30]})
df.plot.bar(x="lab", y="val", rot=0, color = ["y","c","m"])
Result:
Here is a LINK to the some more about the one-letter color abbreviations

Using Bokeh for a dropdown menu which would create different charts

I am trying to create a dropdown interface for my work. My dataset looks like this, it is a random dataset
Now I would like 2 dropdowns say CNN and BBC here. After selecting a channel from dropdown, I would like to select a Topic which would produce a bar chart according to it's value.
I am trying to access just one value initially, but it gives me a blank graph.
from bokeh.plotting import figure
from bokeh.io import output_notebook,show,output_file
p=figure()
import csv
data = [row for row in csv.reader(open('C:/Users/Aishwarya/Documents/books/books_q4/crowd_computing/Bokeh-Python-Visualization-master/interactive/data/data.csv', 'r',encoding="utf8"))]
p.vbar(x=data[1][2], width=0.5, bottom=0,
top=data[1][1], color="firebrick")
#output_notebook()
output_file('1.html')
show(p)
There are probably two issues going on:
The first is that if you are using categorical coordinates on an axis, e.g. "CNN" which it appears you are expecting to use, then you need to etll Bokeh what the categorical range is:
p.figure(x_range=["CNN", ...]) # list all the factors for x_range
If you need to update the axis later you can update the range directly:
p.x_range.factors = [...]
Additionally, as of Bokeh 0.13.0 there is a current open issue that prevents "single" factors from working as coordinates: #6660 Coordinates should accept single categorical values. The upshot is that you will have to put the data in a Bokeh ColumnDataSource explicityl (always an option), or in this case a workaround is also just to pass a single-item list instead:
p.vbar(x=["cnn"], ...)
Here is a complete update of your code, with some fake data put in:
from bokeh.plotting import figure
from bokeh.io import show
p = figure(x_range=["cnn"])
p.vbar(x=["cnn"], width=0.5, bottom=0, top=10, color="firebrick")
show(p)
I would also recommend studying the User's guide section Handling Categorical Data.

Bokeh time series plotting

I have a bunch of time series objects I'm charting with bokeh.charts.TimeSeries data that I want to make into a beautiful plot with a description and title, etc. How can I add a chart to a bokeh.plotting.figure object? I'm using bokeh.layouts.row to organise them, but I want to make it look more professional than a webpage with nothing but a chart.
Is this possible? I was looking at the plotting interface, but I don't see a time series API. Would I just use my pandas.Series objects as the data for the line API?
The old bokeh.charts API, including TimeSeries was deprecated and subsequently removed. You can and should plot time series using the stable bokeh.plotting API. Here is a complete example created with Bokeh 0.13.0:
from bokeh.plotting import figure, show
from bokeh.sampledata.glucose import data
p = figure(x_axis_type="datetime", title="Glocose Range", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'
p.line(week.index, week.glucose)
show(p)

How To Position Chart in Workbook

I am running python2.7, and using openpyxl version 1.8.6. I am able to generate a chart just fine, but am unable to locate anything that indicates the chart can then be positioned in a particular location in a sheet. Any assistance would be appreciated.
I am using the following code to generate the chart:
ws = wb2.get_sheet_by_name('Traffic Data')
rowcount = ws.get_highest_row()
values = Reference(ws,(1,1), (rowcount - 1,1))
labels = Reference(ws,(0,1),(rowcount,0))
title = "Events recorded in " + str(datetime.datetime.strptime(str(runmonth), '%m').strftime('%B'))
series = Series(values, title=title)
chart.add_series(series)
ws = wb2.get_sheet_by_name('Traffic Incidents')
ws.add_chart(chart)
According to the source code it is not possible to set the exact location of the chart while adding it to the worksheet using add_chart().
Consider switching to the xlsxwriter module. Quote from Working with Charts:
# Create a new chart object.
chart = workbook.add_chart({'type': 'line'})
# Add a series to the chart.
chart.add_series({'values': '=Sheet1!$A$1:$A$6'})
# Insert the chart into the worksheet.
worksheet.insert_chart('C1', chart)
I was able to offset the chart position by using chart.drawing. Specifically, I used:
chart.drawing.left = 1000
This offset the chart by about 11 cells. I'm not sure about the pixel to cell mapping, but it can be easily reverse engineered by a few trials.
It's actually really difficult to set the position of any object in Excel because it uses an absurdly stupid system for cell widths: these are based on the width of a number of characters in a particular font and size plus some padding. Charts on the other hand have to be positioned using pixels. We do have the point_pos() method in worksheets but I wouldn't trust it. We've just release 2.0 which has cleaner code. Basically you can play around with the chart.drawing attributes to get it where you need at in what size.
Take look at the code in charts/chart.py for more information. The next release should be a bit more comfortable.

Categories